- move data structures to shared header
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.c
1 /*
2   This file is part of GNUnet.
3   (C) 2012 Christian Grothoff (and other contributing authors)
4
5   GNUnet is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published
7   by the Free Software Foundation; either version 2, or (at your
8   option) any later version.
9
10   GNUnet is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with GNUnet; see the file COPYING.  If not, write to the
17   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18   Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file testbed/gnunet-service-testbed.c
23  * @brief implementation of the TESTBED service
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet-service-testbed.h"
29
30 #include <zlib.h>
31
32
33 /**
34  * The message queue for sending messages to clients
35  */
36 struct MessageQueue
37 {
38   /**
39    * The message to be sent
40    */
41   struct GNUNET_MessageHeader *msg;
42
43   /**
44    * The client to send the message to
45    */
46   struct GNUNET_SERVER_Client *client;
47
48   /**
49    * next pointer for DLL
50    */
51   struct MessageQueue *next;
52
53   /**
54    * prev pointer for DLL
55    */
56   struct MessageQueue *prev;
57 };
58
59
60 /**
61  * The master context; generated with the first INIT message
62  */
63 static struct Context *master_context;
64
65 /**
66  * Our hostname; we give this to all the peers we start
67  */
68 static char *hostname;
69
70
71 /***********/
72 /* Handles */
73 /***********/
74
75 /**
76  * Our configuration
77  */
78 static struct GNUNET_CONFIGURATION_Handle *our_config;
79
80 /**
81  * Current Transmit Handle; NULL if no notify transmit exists currently
82  */
83 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
84
85 /****************/
86 /* Lists & Maps */
87 /****************/
88
89 /**
90  * The head for the LCF queue
91  */
92 static struct LCFContextQueue *lcfq_head;
93
94 /**
95  * The tail for the LCF queue
96  */
97 static struct LCFContextQueue *lcfq_tail;
98
99 /**
100  * The message queue head
101  */
102 static struct MessageQueue *mq_head;
103
104 /**
105  * The message queue tail
106  */
107 static struct MessageQueue *mq_tail;
108
109 /**
110  * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
111  */
112 static struct OverlayConnectContext *occq_head;
113
114 /**
115  * DLL tail for OverlayConnectContext DLL
116  */
117 static struct OverlayConnectContext *occq_tail;
118
119 /**
120  * DLL head for RequectOverlayConnectContext DLL - to be used to clean up during
121  * shutdown
122  */
123 static struct RequestOverlayConnectContext *roccq_head;
124
125 /**
126  * DLL tail for RequectOverlayConnectContext DLL
127  */
128 static struct RequestOverlayConnectContext *roccq_tail;
129
130 /**
131  * DLL head for forwarded operation contexts
132  */
133 static struct ForwardedOperationContext *fopcq_head;
134
135 /**
136  * DLL tail for forwarded operation contexts
137  */
138 static struct ForwardedOperationContext *fopcq_tail;
139
140 /**
141  * DLL head for least recently used hello cache entries; least recently used
142  * cache items are at the head
143  */
144 static struct HelloCacheEntry *lru_hcache_head;
145
146 /**
147  * DLL tail for least recently used hello cache entries; recently used cache
148  * items are at the tail
149  */
150 static struct HelloCacheEntry *lru_hcache_tail;
151
152 /**
153  * Array of hosts
154  */
155 static struct GNUNET_TESTBED_Host **host_list;
156
157 /**
158  * A list of routes
159  */
160 static struct Route **route_list;
161
162 /**
163  * A list of directly linked neighbours
164  */
165 static struct Slave **slave_list;
166
167 /**
168  * A list of peers we know about
169  */
170 static struct Peer **peer_list;
171
172 /**
173  * The hashmap of shared services
174  */
175 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
176
177 /**
178  * Hashmap to maintain HELLO cache
179  */
180 static struct GNUNET_CONTAINER_MultiHashMap *hello_cache;
181
182 /**
183  * The event mask for the events we listen from sub-controllers
184  */
185 static uint64_t event_mask;
186
187 /**
188  * The size of the host list
189  */
190 static unsigned int host_list_size;
191
192 /**
193  * The size of the route list
194  */
195 static unsigned int route_list_size;
196
197 /**
198  * The size of directly linked neighbours list
199  */
200 static unsigned int slave_list_size;
201
202 /**
203  * The size of the peer list
204  */
205 static unsigned int peer_list_size;
206
207 /**
208  * The size of hello cache
209  */
210 static unsigned int hello_cache_size;
211
212 /*********/
213 /* Tasks */
214 /*********/
215
216 /**
217  * The lcf_task handle
218  */
219 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
220
221 /**
222  * The shutdown task handle
223  */
224 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
225
226
227 /**
228  * Looks up in the hello cache and returns the HELLO of the given peer
229  *
230  * @param id the peer identity of the peer whose HELLO has to be looked up
231  * @return the HELLO message; NULL if not found
232  */
233 static const struct GNUNET_MessageHeader *
234 hello_cache_lookup (const struct GNUNET_PeerIdentity *id)
235 {
236   struct HelloCacheEntry *entry;
237
238   if (NULL == hello_cache)
239     return NULL;
240   entry = GNUNET_CONTAINER_multihashmap_get (hello_cache, &id->hashPubKey);
241   if (NULL == entry)
242     return NULL;
243   GNUNET_CONTAINER_DLL_remove (lru_hcache_head, lru_hcache_tail, entry);
244   GNUNET_CONTAINER_DLL_insert_tail (lru_hcache_head, lru_hcache_tail, entry);
245   return entry->hello;
246 }
247
248
249 /**
250  * Removes the given hello cache centry from hello cache and frees its resources
251  *
252  * @param entry the entry to remove
253  */
254 static void
255 hello_cache_remove (struct HelloCacheEntry *entry)
256 {
257   GNUNET_CONTAINER_DLL_remove (lru_hcache_head, lru_hcache_tail, entry);
258   GNUNET_assert (GNUNET_YES == 
259                  GNUNET_CONTAINER_multihashmap_remove (hello_cache,
260                                                        &entry->key,
261                                                        entry));
262   GNUNET_free (entry->hello);
263   GNUNET_free (entry);
264 }
265
266
267 /**
268  * Caches the HELLO of the given peer. Updates the HELLO if it was already
269  * cached before
270  *
271  * @param id the peer identity of the peer whose HELLO has to be cached
272  * @param hello the HELLO message
273  */
274 static void
275 hello_cache_add (const struct GNUNET_PeerIdentity *id,
276                  const struct GNUNET_MessageHeader *hello)
277 {
278   struct HelloCacheEntry *entry;
279   
280   if (NULL == hello_cache)
281     return;
282   entry = GNUNET_CONTAINER_multihashmap_get (hello_cache, &id->hashPubKey);
283   if (NULL == entry)
284   {
285     entry = GNUNET_malloc (sizeof (struct HelloCacheEntry));
286     memcpy (&entry->key, &id->hashPubKey, sizeof (struct GNUNET_HashCode));
287     if (GNUNET_CONTAINER_multihashmap_size (hello_cache) == hello_cache_size)
288     {
289       GNUNET_assert (NULL != lru_hcache_head);
290       hello_cache_remove (lru_hcache_head);
291     }
292     GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put 
293                    (hello_cache,
294                     &entry->key,
295                     entry,
296                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
297   }
298   else
299   {
300     GNUNET_CONTAINER_DLL_remove (lru_hcache_head, lru_hcache_tail, entry);
301     GNUNET_free (entry->hello);
302   }
303   entry->hello = GNUNET_copy_message (hello);
304   GNUNET_CONTAINER_DLL_insert_tail (lru_hcache_head, lru_hcache_tail, entry);
305 }
306
307
308 /**
309  * Function called to notify a client about the connection begin ready to queue
310  * more data.  "buf" will be NULL and "size" zero if the connection was closed
311  * for writing in the meantime.
312  *
313  * @param cls NULL
314  * @param size number of bytes available in buf
315  * @param buf where the callee should write the message
316  * @return number of bytes written to buf
317  */
318 static size_t
319 transmit_ready_notify (void *cls, size_t size, void *buf)
320 {
321   struct MessageQueue *mq_entry;
322
323   transmit_handle = NULL;
324   mq_entry = mq_head;
325   GNUNET_assert (NULL != mq_entry);
326   if (0 == size)
327     return 0;
328   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
329   size = ntohs (mq_entry->msg->size);
330   memcpy (buf, mq_entry->msg, size);
331   GNUNET_free (mq_entry->msg);
332   GNUNET_SERVER_client_drop (mq_entry->client);
333   GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
334   GNUNET_free (mq_entry);
335   mq_entry = mq_head;
336   if (NULL != mq_entry)
337     transmit_handle =
338         GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
339                                              ntohs (mq_entry->msg->size),
340                                              GNUNET_TIME_UNIT_FOREVER_REL,
341                                              &transmit_ready_notify, NULL);
342   return size;
343 }
344
345
346 /**
347  * Queues a message in send queue for sending to the service
348  *
349  * @param client the client to whom the queued message has to be sent
350  * @param msg the message to queue
351  */
352 static void
353 queue_message (struct GNUNET_SERVER_Client *client,
354                struct GNUNET_MessageHeader *msg)
355 {
356   struct MessageQueue *mq_entry;
357   uint16_t type;
358   uint16_t size;
359
360   type = ntohs (msg->type);
361   size = ntohs (msg->size);
362   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
363                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
364   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
365   mq_entry->msg = msg;
366   mq_entry->client = client;
367   GNUNET_SERVER_client_keep (client);
368   LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
369              ntohs (msg->size));
370   GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
371   if (NULL == transmit_handle)
372     transmit_handle =
373         GNUNET_SERVER_notify_transmit_ready (client, size,
374                                              GNUNET_TIME_UNIT_FOREVER_REL,
375                                              &transmit_ready_notify, NULL);
376 }
377
378
379 /**
380  * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
381  * several times we call it only once. The array is also made to grow in steps
382  * of LIST_GROW_STEP.
383  *
384  * @param ptr the array pointer to grow
385  * @param size the size of array
386  * @param accommodate_size the size which the array has to accommdate; after
387  *          this call the array will be big enough to accommdate sizes upto
388  *          accommodate_size
389  */
390 #define array_grow_large_enough(ptr, size, accommodate_size) \
391   do                                                                    \
392   {                                                                     \
393     unsigned int growth_size;                                           \
394     GNUNET_assert (size <= accommodate_size);                            \
395     growth_size = size;                                                 \
396     while (growth_size <= accommodate_size)                             \
397       growth_size += LIST_GROW_STEP;                                    \
398     GNUNET_array_grow (ptr, size, growth_size);                         \
399     GNUNET_assert (size > accommodate_size);                            \
400   } while (0)
401
402
403 /**
404  * Function to add a host to the current list of known hosts
405  *
406  * @param host the host to add
407  * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
408  *           already in use
409  */
410 static int
411 host_list_add (struct GNUNET_TESTBED_Host *host)
412 {
413   uint32_t host_id;
414
415   host_id = GNUNET_TESTBED_host_get_id_ (host);
416   if (host_list_size <= host_id)
417     array_grow_large_enough (host_list, host_list_size, host_id);
418   if (NULL != host_list[host_id])
419   {
420     LOG_DEBUG ("A host with id: %u already exists\n", host_id);
421     return GNUNET_SYSERR;
422   }
423   host_list[host_id] = host;
424   return GNUNET_OK;
425 }
426
427
428 /**
429  * Adds a route to the route list
430  *
431  * @param route the route to add
432  */
433 static void
434 route_list_add (struct Route *route)
435 {
436   if (route->dest >= route_list_size)
437     array_grow_large_enough (route_list, route_list_size, route->dest);
438   GNUNET_assert (NULL == route_list[route->dest]);
439   route_list[route->dest] = route;
440 }
441
442
443 /**
444  * Adds a slave to the slave array
445  *
446  * @param slave the slave controller to add
447  */
448 static void
449 slave_list_add (struct Slave *slave)
450 {
451   if (slave->host_id >= slave_list_size)
452     array_grow_large_enough (slave_list, slave_list_size, slave->host_id);
453   GNUNET_assert (NULL == slave_list[slave->host_id]);
454   slave_list[slave->host_id] = slave;
455 }
456
457
458 /**
459  * Adds a peer to the peer array
460  *
461  * @param peer the peer to add
462  */
463 static void
464 peer_list_add (struct Peer *peer)
465 {
466   if (peer->id >= peer_list_size)
467     array_grow_large_enough (peer_list, peer_list_size, peer->id);
468   GNUNET_assert (NULL == peer_list[peer->id]);
469   peer_list[peer->id] = peer;
470 }
471
472
473 /**
474  * Removes a the give peer from the peer array
475  *
476  * @param peer the peer to be removed
477  */
478 static void
479 peer_list_remove (struct Peer *peer)
480 {
481   unsigned int orig_size;
482   uint32_t id;
483
484   peer_list[peer->id] = NULL;
485   orig_size = peer_list_size;
486   while (peer_list_size >= LIST_GROW_STEP)
487   {
488     for (id = peer_list_size - 1;
489          (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
490       if (NULL != peer_list[id])
491         break;
492     if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
493       break;
494     peer_list_size -= LIST_GROW_STEP;
495   }
496   if (orig_size == peer_list_size)
497     return;
498   peer_list =
499       GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
500 }
501
502
503 /**
504  * Finds the route with directly connected host as destination through which
505  * the destination host can be reached
506  *
507  * @param host_id the id of the destination host
508  * @return the route with directly connected destination host; NULL if no route
509  *           is found
510  */
511 static struct Route *
512 find_dest_route (uint32_t host_id)
513 {
514   struct Route *route;
515
516   if (route_list_size <= host_id)
517     return NULL;
518   while (NULL != (route = route_list[host_id]))
519   {
520     if (route->thru == master_context->host_id)
521       break;
522     host_id = route->thru;
523   }
524   return route;
525 }
526
527
528 /**
529  * Routes message to a host given its host_id
530  *
531  * @param host_id the id of the destination host
532  * @param msg the message to be routed
533  */
534 static void
535 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
536 {
537   GNUNET_break (0);
538 }
539
540
541 /**
542  * Send operation failure message to client
543  *
544  * @param client the client to which the failure message has to be sent to
545  * @param operation_id the id of the failed operation
546  * @param emsg the error message; can be NULL
547  */
548 static void
549 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
550                          uint64_t operation_id, const char *emsg)
551 {
552   struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
553   uint16_t msize;
554   uint16_t emsg_len;
555
556   msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
557   emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
558   msize += emsg_len;
559   msg = GNUNET_malloc (msize);
560   msg->header.size = htons (msize);
561   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
562   msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
563   msg->operation_id = GNUNET_htonll (operation_id);
564   if (0 != emsg_len)
565     memcpy (&msg[1], emsg, emsg_len);
566   queue_message (client, &msg->header);
567 }
568
569
570 /**
571  * Function to send generic operation success message to given client
572  *
573  * @param client the client to send the message to
574  * @param operation_id the id of the operation which was successful
575  */
576 static void
577 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
578                             uint64_t operation_id)
579 {
580   struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
581   uint16_t msize;
582
583   msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
584   msg = GNUNET_malloc (msize);
585   msg->header.size = htons (msize);
586   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
587   msg->operation_id = GNUNET_htonll (operation_id);
588   msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
589   queue_message (client, &msg->header);
590 }
591
592
593 /**
594  * Callback which will be called to after a host registration succeeded or failed
595  *
596  * @param cls the handle to the slave at which the registration is completed
597  * @param emsg the error message; NULL if host registration is successful
598  */
599 static void 
600 hr_completion (void *cls, const char *emsg);
601
602
603 /**
604  * Attempts to register the next host in the host registration queue
605  *
606  * @param slave the slave controller whose host registration queue is checked
607  *          for host registrations
608  */
609 static void
610 register_next_host (struct Slave *slave)
611 {
612   struct HostRegistration *hr;
613   
614   hr = slave->hr_dll_head;
615   GNUNET_assert (NULL != hr);
616   GNUNET_assert (NULL == slave->rhandle);
617   LOG (GNUNET_ERROR_TYPE_DEBUG,
618        "Registering host %u at %u\n",
619        GNUNET_TESTBED_host_get_id_ (hr->host),
620        GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
621   slave->rhandle = GNUNET_TESTBED_register_host (slave->controller,
622                                                  hr->host,
623                                                  hr_completion,
624                                                  slave);
625 }
626
627
628 /**
629  * Callback which will be called to after a host registration succeeded or failed
630  *
631  * @param cls the handle to the slave at which the registration is completed
632  * @param emsg the error message; NULL if host registration is successful
633  */
634 static void 
635 hr_completion (void *cls, const char *emsg)
636 {
637   struct Slave *slave = cls;
638   struct HostRegistration *hr;
639
640   slave->rhandle = NULL;
641   hr = slave->hr_dll_head;
642   GNUNET_assert (NULL != hr);
643   LOG (GNUNET_ERROR_TYPE_DEBUG,
644        "Registering host %u at %u successful\n",
645        GNUNET_TESTBED_host_get_id_ (hr->host),
646        GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
647   GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head,
648                                slave->hr_dll_tail,
649                                hr);
650   if (NULL != hr->cb)
651     hr->cb (hr->cb_cls, emsg);
652   GNUNET_free (hr);
653   if (NULL != slave->hr_dll_head)
654     register_next_host (slave);
655 }
656
657
658 /**
659  * Adds a host registration's request to a slave's registration queue
660  *
661  * @param slave the slave controller at which the given host has to be
662  *          registered 
663  * @param cb the host registration completion callback
664  * @param cb_cls the closure for the host registration completion callback
665  * @param host the host which has to be registered
666  */
667 static void
668 queue_host_registration (struct Slave *slave,
669                          GNUNET_TESTBED_HostRegistrationCompletion cb,
670                          void *cb_cls,
671                          struct GNUNET_TESTBED_Host *host)
672 {
673   struct HostRegistration *hr;
674   int call_register;
675
676   LOG (GNUNET_ERROR_TYPE_DEBUG,
677        "Queueing host registration for host %u at %u\n",
678        GNUNET_TESTBED_host_get_id_ (host),
679        GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
680   hr = GNUNET_malloc (sizeof (struct HostRegistration));
681   hr->cb = cb;
682   hr->cb_cls = cb_cls;
683   hr->host = host;
684   call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
685   GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head,
686                                     slave->hr_dll_tail,
687                                     hr);
688   if (GNUNET_YES == call_register)
689     register_next_host (slave);
690 }
691
692
693 /**
694  * The  Link Controller forwarding task
695  *
696  * @param cls the LCFContext
697  * @param tc the Task context from scheduler
698  */
699 static void
700 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
701
702
703 /**
704  * Completion callback for host registrations while forwarding Link Controller messages
705  *
706  * @param cls the LCFContext
707  * @param emsg the error message; NULL if host registration is successful
708  */
709 static void
710 lcf_proc_cc (void *cls, const char *emsg)
711 {
712   struct LCFContext *lcf = cls;
713
714   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
715   switch (lcf->state)
716   {
717   case INIT:
718     if (NULL != emsg)
719       goto registration_error;
720     lcf->state = DELEGATED_HOST_REGISTERED;
721     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
722     break;
723   case DELEGATED_HOST_REGISTERED:
724     if (NULL != emsg)
725       goto registration_error;
726     lcf->state = SLAVE_HOST_REGISTERED;
727     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
728     break;
729   default:
730     GNUNET_assert (0);          /* Shouldn't reach here */
731   }
732   return;
733
734  registration_error:
735   LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
736        emsg);
737   lcf->state = FINISHED;
738   lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
739 }
740
741
742 /**
743  * Callback to relay the reply msg of a forwarded operation back to the client
744  *
745  * @param cls ForwardedOperationContext
746  * @param msg the message to relay
747  */
748 static void
749 forwarded_operation_reply_relay (void *cls,
750                                  const struct GNUNET_MessageHeader *msg)
751 {
752   struct ForwardedOperationContext *fopc = cls;
753   struct GNUNET_MessageHeader *dup_msg;
754   uint16_t msize;
755
756   msize = ntohs (msg->size);
757   LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
758              msize);
759   dup_msg = GNUNET_copy_message (msg);
760   queue_message (fopc->client, dup_msg);
761   GNUNET_SERVER_client_drop (fopc->client);
762   GNUNET_SCHEDULER_cancel (fopc->timeout_task);
763   GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
764   GNUNET_free (fopc);
765 }
766
767
768 /**
769  * Task to free resources when forwarded operation has been timedout
770  *
771  * @param cls the ForwardedOperationContext
772  * @param tc the task context from scheduler
773  */
774 static void
775 forwarded_operation_timeout (void *cls,
776                              const struct GNUNET_SCHEDULER_TaskContext *tc)
777 {
778   struct ForwardedOperationContext *fopc = cls;
779
780   GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
781   LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
782   send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
783   GNUNET_SERVER_client_drop (fopc->client);
784   GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
785   GNUNET_free (fopc);
786 }
787
788
789 /**
790  * The  Link Controller forwarding task
791  *
792  * @param cls the LCFContext
793  * @param tc the Task context from scheduler
794  */
795 static void
796 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
797
798
799 /**
800  * Callback to be called when forwarded link controllers operation is
801  * successfull. We have to relay the reply msg back to the client
802  *
803  * @param cls the LCFContext
804  * @param msg the message to relay
805  */
806 static void
807 lcf_forwarded_operation_reply_relay (void *cls,
808                                      const struct GNUNET_MessageHeader *msg)
809 {
810   struct LCFContext *lcf = cls;
811
812   GNUNET_assert (NULL != lcf->fopc);
813   forwarded_operation_reply_relay (lcf->fopc, msg);
814   lcf->fopc = NULL;
815   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
816   lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
817 }
818
819
820 /**
821  * Task to free resources when forwarded link controllers has been timedout
822  *
823  * @param cls the LCFContext
824  * @param tc the task context from scheduler
825  */
826 static void
827 lcf_forwarded_operation_timeout (void *cls,
828                                  const struct GNUNET_SCHEDULER_TaskContext *tc)
829 {
830   struct LCFContext *lcf = cls;
831
832   GNUNET_assert (NULL != lcf->fopc);
833   lcf->fopc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
834   forwarded_operation_timeout (lcf->fopc, tc);
835   lcf->fopc = NULL;
836   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
837   lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
838 }
839
840
841 /**
842  * The  Link Controller forwarding task
843  *
844  * @param cls the LCFContext
845  * @param tc the Task context from scheduler
846  */
847 static void
848 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
849 {
850   struct LCFContext *lcf = cls;
851   struct LCFContextQueue *lcfq;
852
853   lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
854   switch (lcf->state)
855   {
856   case INIT:
857     if (GNUNET_NO ==
858         GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
859                                             lcf->gateway->controller))
860     {
861       queue_host_registration (lcf->gateway,
862                                lcf_proc_cc, lcf,
863                                host_list[lcf->delegated_host_id]);
864     }
865     else
866     {
867       lcf->state = DELEGATED_HOST_REGISTERED;
868       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
869     }
870     break;
871   case DELEGATED_HOST_REGISTERED:
872     if (GNUNET_NO ==
873         GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
874                                             lcf->gateway->controller))
875     {
876       queue_host_registration (lcf->gateway,
877                                lcf_proc_cc, lcf,
878                                host_list[lcf->slave_host_id]);
879     }
880     else
881     {
882       lcf->state = SLAVE_HOST_REGISTERED;
883       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
884     }
885     break;
886   case SLAVE_HOST_REGISTERED:
887     lcf->fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
888     lcf->fopc->client = lcf->client;
889     lcf->fopc->operation_id = lcf->operation_id;
890     lcf->fopc->type = OP_LINK_CONTROLLERS;
891     lcf->fopc->opc =
892         GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
893                                                lcf->operation_id,
894                                                &lcf->msg->header,
895                                                &lcf_forwarded_operation_reply_relay,
896                                                lcf);
897     lcf->fopc->timeout_task =
898         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &lcf_forwarded_operation_timeout,
899                                       lcf);
900     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, lcf->fopc);
901     lcf->state = FINISHED;
902     break;
903   case FINISHED:
904     lcfq = lcfq_head;
905     GNUNET_assert (lcfq->lcf == lcf);
906     GNUNET_free (lcf->msg);
907     GNUNET_free (lcf);
908     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
909     GNUNET_free (lcfq);
910     if (NULL != lcfq_head)
911       lcf_proc_task_id =
912           GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
913   }
914 }
915
916
917 /**
918  * Cleans up ForwardedOverlayConnectContext
919  *
920  * @param focc the ForwardedOverlayConnectContext to cleanup
921  */
922 static void
923 cleanup_focc (struct ForwardedOverlayConnectContext *focc)
924 {
925   GNUNET_free_non_null (focc->orig_msg);
926   GNUNET_free (focc);
927 }
928
929
930 /**
931  * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
932  *
933  * @param rhc the RegisteredHostContext
934  */
935 static void
936 process_next_focc (struct RegisteredHostContext *rhc);
937
938
939 /**
940  * Timeout task for cancelling a forwarded overlay connect connect
941  *
942  * @param cls the ForwardedOverlayConnectContext
943  * @param tc the task context from the scheduler
944  */
945 static void
946 forwarded_overlay_connect_timeout (void *cls,
947                                    const struct GNUNET_SCHEDULER_TaskContext
948                                    *tc)
949 {
950   struct ForwardedOperationContext *fopc = cls;
951   struct RegisteredHostContext *rhc;
952   struct ForwardedOverlayConnectContext *focc;
953   
954   rhc = fopc->cls;
955   focc = rhc->focc_dll_head;
956   GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
957   cleanup_focc (focc);
958   LOG_DEBUG ("Overlay linking between peers %u and %u failed\n",
959              focc->peer1, focc->peer2);
960   forwarded_operation_timeout (cls, tc);
961   if (NULL != rhc->focc_dll_head)
962     process_next_focc (rhc);
963 }
964
965
966 /**
967  * Callback to be called when forwarded overlay connection operation has a reply
968  * from the sub-controller successfull. We have to relay the reply msg back to
969  * the client
970  *
971  * @param cls ForwardedOperationContext
972  * @param msg the peer create success message
973  */
974 static void
975 forwarded_overlay_connect_listener (void *cls,
976                                     const struct GNUNET_MessageHeader *msg)
977 {
978   struct ForwardedOperationContext *fopc = cls;
979   struct RegisteredHostContext *rhc;
980   struct ForwardedOverlayConnectContext *focc;
981   
982   rhc = fopc->cls;
983   forwarded_operation_reply_relay (cls, msg);
984   focc = rhc->focc_dll_head;
985   GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
986   cleanup_focc (focc);
987   if (NULL != rhc->focc_dll_head)
988     process_next_focc (rhc);
989 }
990
991
992 /**
993  * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
994  *
995  * @param rhc the RegisteredHostContext
996  */
997 static void
998 process_next_focc (struct RegisteredHostContext *rhc)
999 {
1000   struct ForwardedOperationContext *fopc;
1001   struct ForwardedOverlayConnectContext *focc;
1002
1003   focc = rhc->focc_dll_head;
1004   GNUNET_assert (NULL != focc);
1005   GNUNET_assert (RHC_OL_CONNECT == rhc->state);
1006   fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1007   GNUNET_SERVER_client_keep (rhc->client);
1008   fopc->client = rhc->client;  
1009   fopc->operation_id = focc->operation_id;
1010   fopc->cls = rhc;
1011   fopc->type = OP_OVERLAY_CONNECT;
1012   fopc->opc =
1013       GNUNET_TESTBED_forward_operation_msg_ (rhc->gateway->controller,
1014                                              focc->operation_id, focc->orig_msg,
1015                                              &forwarded_overlay_connect_listener,
1016                                              fopc);
1017   GNUNET_free (focc->orig_msg);
1018   focc->orig_msg = NULL;
1019   fopc->timeout_task =
1020       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_overlay_connect_timeout,
1021                                     fopc);
1022   GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1023 }
1024
1025
1026 /**
1027  * Callback for event from slave controllers
1028  *
1029  * @param cls struct Slave *
1030  * @param event information about the event
1031  */
1032 static void
1033 slave_event_callback (void *cls,
1034                       const struct GNUNET_TESTBED_EventInformation *event)
1035 {
1036   struct RegisteredHostContext *rhc;
1037   struct GNUNET_CONFIGURATION_Handle *cfg;
1038   struct GNUNET_TESTBED_Operation *old_op;
1039   
1040   /* We currently only get here when working on RegisteredHostContexts */
1041   GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
1042   rhc = event->details.operation_finished.op_cls;
1043   GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
1044   switch (rhc->state)
1045   {
1046   case RHC_GET_CFG:
1047     cfg = event->details.operation_finished.generic;
1048     old_op = rhc->sub_op;
1049     rhc->state = RHC_LINK;
1050     rhc->sub_op =
1051         GNUNET_TESTBED_controller_link (rhc,
1052                                         rhc->gateway->controller,
1053                                         rhc->reg_host,
1054                                         rhc->host,
1055                                         cfg,
1056                                         GNUNET_NO);
1057     GNUNET_TESTBED_operation_done (old_op);
1058     break;
1059   case RHC_LINK:
1060     LOG_DEBUG ("OL: Linking controllers successfull\n");
1061     GNUNET_TESTBED_operation_done (rhc->sub_op);
1062     rhc->sub_op = NULL;
1063     rhc->state = RHC_OL_CONNECT;
1064     process_next_focc (rhc);
1065     break;
1066   default:
1067     GNUNET_assert (0);
1068   }
1069 }
1070
1071
1072 /**
1073  * Callback to signal successfull startup of the controller process
1074  *
1075  * @param cls the handle to the slave whose status is to be found here
1076  * @param cfg the configuration with which the controller has been started;
1077  *          NULL if status is not GNUNET_OK
1078  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1079  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1080  */
1081 static void
1082 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1083                        int status)
1084 {
1085   struct Slave *slave = cls;
1086   struct LinkControllersContext *lcc;
1087
1088   lcc = slave->lcc;
1089   if (GNUNET_SYSERR == status)
1090   {
1091     slave->controller_proc = NULL;
1092     slave_list[slave->host_id] = NULL;
1093     if (NULL != slave->cfg)
1094       GNUNET_CONFIGURATION_destroy (slave->cfg);
1095     GNUNET_free (slave);
1096     slave = NULL;
1097     LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1098     GNUNET_SCHEDULER_shutdown ();       /* We too shutdown */
1099     goto clean_lcc;
1100   }
1101   slave->controller =
1102       GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1103                                          event_mask,
1104                                          &slave_event_callback, slave);
1105   if (NULL != slave->controller)
1106   {
1107     send_operation_success_msg (lcc->client, lcc->operation_id);
1108     slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1109   }
1110   else
1111   {
1112     send_operation_fail_msg (lcc->client, lcc->operation_id,
1113                              "Could not connect to delegated controller");
1114     GNUNET_TESTBED_controller_stop (slave->controller_proc);
1115     slave_list[slave->host_id] = NULL;
1116     GNUNET_free (slave);
1117     slave = NULL;
1118   }
1119
1120  clean_lcc:
1121   if (NULL != lcc)
1122   {
1123     if (NULL != lcc->client)
1124     {
1125       GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1126       GNUNET_SERVER_client_drop (lcc->client);
1127       lcc->client = NULL;
1128     }
1129     GNUNET_free (lcc);
1130   }
1131   if (NULL != slave)
1132     slave->lcc = NULL;
1133 }
1134
1135
1136 /**
1137  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1138  *
1139  * @param cls NULL
1140  * @param client identification of the client
1141  * @param message the actual message
1142  */
1143 static void
1144 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1145              const struct GNUNET_MessageHeader *message)
1146 {
1147   const struct GNUNET_TESTBED_InitMessage *msg;
1148   struct GNUNET_TESTBED_Host *host;
1149   const char *controller_hostname;
1150   uint16_t msize;
1151
1152   if (NULL != master_context)
1153   {
1154     LOG_DEBUG ("We are being connected to laterally\n");
1155     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1156     return;
1157   }
1158   msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1159   msize = ntohs (message->size);
1160   if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1161   {
1162     GNUNET_break (0);
1163     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1164     return;
1165   }
1166   msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1167   controller_hostname = (const char *) &msg[1];
1168   if ('\0' != controller_hostname[msize - 1])
1169   {
1170     GNUNET_break (0);
1171     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1172     return;
1173   }
1174   master_context = GNUNET_malloc (sizeof (struct Context));
1175   GNUNET_SERVER_client_keep (client);
1176   master_context->client = client;
1177   master_context->host_id = ntohl (msg->host_id);
1178   master_context->master_ip = GNUNET_strdup (controller_hostname);
1179   LOG_DEBUG ("Our IP: %s\n", master_context->master_ip);
1180   master_context->system =
1181       GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1182   host =
1183       GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1184                                           master_context->master_ip,
1185                                           NULL,
1186                                           0);
1187   host_list_add (host);
1188   LOG_DEBUG ("Created master context with host ID: %u\n",
1189              master_context->host_id);
1190   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1191 }
1192
1193
1194 /**
1195  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1196  *
1197  * @param cls NULL
1198  * @param client identification of the client
1199  * @param message the actual message
1200  */
1201 static void
1202 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1203                  const struct GNUNET_MessageHeader *message)
1204 {
1205   struct GNUNET_TESTBED_Host *host;
1206   const struct GNUNET_TESTBED_AddHostMessage *msg;
1207   struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1208   char *username;
1209   char *hostname;
1210   char *emsg;
1211   uint32_t host_id;
1212   uint16_t username_length;
1213   uint16_t hostname_length;
1214   uint16_t reply_size;
1215   uint16_t msize;
1216
1217   msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1218   msize = ntohs (msg->header.size);
1219   username = (char *) &msg[1];
1220   username_length = ntohs (msg->user_name_length);
1221   if (0 != username_length)
1222     username_length++;
1223   /* msg must contain hostname */
1224   GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1225                           username_length + 1));
1226   if (0 != username_length)
1227     GNUNET_assert ('\0' == username[username_length - 1]);
1228   hostname = username + username_length;
1229   hostname_length =
1230       msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1231   GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1232   GNUNET_assert (strlen (hostname) == hostname_length - 1);
1233   host_id = ntohl (msg->host_id);
1234   LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1235   LOG_DEBUG ("-------host id: %u\n", host_id);
1236   LOG_DEBUG ("-------hostname: %s\n", hostname);
1237   if (0 != username_length)
1238     LOG_DEBUG ("-------username: %s\n", username);
1239   else
1240   {
1241     LOG_DEBUG ("-------username: NULL\n");
1242     username = NULL;
1243   }
1244   LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1245   host =
1246       GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1247                                           ntohs (msg->ssh_port));
1248   GNUNET_assert (NULL != host);
1249   reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1250   if (GNUNET_OK != host_list_add (host))
1251   {
1252     /* We are unable to add a host */
1253     emsg = "A host exists with given host-id";
1254     LOG_DEBUG ("%s: %u", emsg, host_id);
1255     GNUNET_TESTBED_host_destroy (host);
1256     reply_size += strlen (emsg) + 1;
1257     reply = GNUNET_malloc (reply_size);
1258     memcpy (&reply[1], emsg, strlen (emsg) + 1);
1259   }
1260   else
1261   {
1262     LOG_DEBUG ("Added host %u at %u\n",
1263                host_id, master_context->host_id);
1264     reply = GNUNET_malloc (reply_size);
1265   }
1266   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1267   reply->header.size = htons (reply_size);
1268   reply->host_id = htonl (host_id);
1269   queue_message (client, &reply->header);
1270   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1271 }
1272
1273
1274 /**
1275  * Iterator over hash map entries.
1276  *
1277  * @param cls closure
1278  * @param key current key code
1279  * @param value value in the hash map
1280  * @return GNUNET_YES if we should continue to
1281  *         iterate,
1282  *         GNUNET_NO if not.
1283  */
1284 int
1285 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1286 {
1287   struct SharedService *queried_ss = cls;
1288   struct SharedService *ss = value;
1289
1290   if (0 == strcmp (ss->name, queried_ss->name))
1291     return GNUNET_NO;
1292   else
1293     return GNUNET_YES;
1294 }
1295
1296
1297 /**
1298  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1299  *
1300  * @param cls NULL
1301  * @param client identification of the client
1302  * @param message the actual message
1303  */
1304 static void
1305 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1306                                  const struct GNUNET_MessageHeader *message)
1307 {
1308   const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1309   struct SharedService *ss;
1310   char *service_name;
1311   struct GNUNET_HashCode hash;
1312   uint16_t msg_size;
1313   uint16_t service_name_size;
1314
1315   msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1316   msg_size = ntohs (message->size);
1317   if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1318   {
1319     GNUNET_break (0);
1320     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1321     return;
1322   }
1323   service_name_size =
1324       msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1325   service_name = (char *) &msg[1];
1326   if ('\0' != service_name[service_name_size - 1])
1327   {
1328     GNUNET_break (0);
1329     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1330     return;
1331   }
1332   LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1333              service_name, ntohl (msg->num_peers));
1334   if (ntohl (msg->host_id) != master_context->host_id)
1335   {
1336     route_message (ntohl (msg->host_id), message);
1337     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1338     return;
1339   }
1340   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1341   ss = GNUNET_malloc (sizeof (struct SharedService));
1342   ss->name = strdup (service_name);
1343   ss->num_shared = ntohl (msg->num_peers);
1344   GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1345   if (GNUNET_SYSERR ==
1346       GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1347                                                   &ss_exists_iterator, ss))
1348   {
1349     LOG (GNUNET_ERROR_TYPE_WARNING,
1350          "Service %s already configured as a shared service. "
1351          "Ignoring service sharing request \n", ss->name);
1352     GNUNET_free (ss->name);
1353     GNUNET_free (ss);
1354     return;
1355   }
1356   GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1357                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1358 }
1359
1360
1361 /**
1362  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1363  *
1364  * @param cls NULL
1365  * @param client identification of the client
1366  * @param message the actual message
1367  */
1368 static void
1369 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1370                          const struct GNUNET_MessageHeader *message)
1371 {
1372   const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1373   struct GNUNET_CONFIGURATION_Handle *cfg;
1374   struct LCFContextQueue *lcfq;
1375   struct Route *route;
1376   struct Route *new_route;
1377   char *config;
1378   uLongf dest_size;
1379   size_t config_size;
1380   uint32_t delegated_host_id;
1381   uint32_t slave_host_id;
1382   uint16_t msize;
1383
1384   if (NULL == master_context)
1385   {
1386     GNUNET_break (0);
1387     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1388     return;
1389   }
1390   msize = ntohs (message->size);
1391   if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1392   {
1393     GNUNET_break (0);
1394     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1395     return;
1396   }
1397   msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1398   delegated_host_id = ntohl (msg->delegated_host_id);
1399   if (delegated_host_id == master_context->host_id)
1400   {
1401     GNUNET_break (0);
1402     LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1403     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1404     return;
1405   }
1406   if ((delegated_host_id >= host_list_size) ||
1407       (NULL == host_list[delegated_host_id]))
1408   {
1409     LOG (GNUNET_ERROR_TYPE_WARNING,
1410          "Delegated host %u not registered with us\n", delegated_host_id);
1411     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1412     return;
1413   }
1414   slave_host_id = ntohl (msg->slave_host_id);
1415   if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1416   {
1417     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1418          slave_host_id);
1419     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1420     return;
1421   }
1422   if (slave_host_id == delegated_host_id)
1423   {
1424     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1425     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1426     return;
1427   }
1428
1429   if (slave_host_id == master_context->host_id) /* Link from us */
1430   {
1431     struct Slave *slave;
1432     struct LinkControllersContext *lcc;
1433
1434     msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1435     config_size = ntohs (msg->config_size);
1436     if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id]))       /* We have already added */
1437     {
1438       LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1439            delegated_host_id);
1440       GNUNET_SERVER_receive_done (client, GNUNET_OK);
1441       return;
1442     }
1443     config = GNUNET_malloc (config_size);
1444     dest_size = (uLongf) config_size;
1445     if (Z_OK !=
1446         uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1447                     (uLong) msize))
1448     {
1449       GNUNET_break (0);         /* Compression error */
1450       GNUNET_free (config);
1451       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1452       return;
1453     }
1454     if (config_size != dest_size)
1455     {
1456       LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1457       GNUNET_free (config);
1458       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1459       return;
1460     }
1461     cfg = GNUNET_CONFIGURATION_create ();       /* Free here or in lcfcontext */
1462     if (GNUNET_OK !=
1463         GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1464     {
1465       GNUNET_break (0);         /* Configuration parsing error */
1466       GNUNET_free (config);
1467       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1468       return;
1469     }
1470     GNUNET_free (config);
1471     if ((delegated_host_id < slave_list_size) &&
1472         (NULL != slave_list[delegated_host_id]))
1473     {
1474       GNUNET_break (0);         /* Configuration parsing error */
1475       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1476       return;
1477     }
1478     slave = GNUNET_malloc (sizeof (struct Slave));
1479     slave->host_id = delegated_host_id;
1480     slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
1481     slave_list_add (slave);
1482     if (1 != msg->is_subordinate)
1483     {
1484       slave->controller =
1485           GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1486                                              event_mask,
1487                                              &slave_event_callback, slave);
1488       slave->cfg = cfg;
1489       if (NULL != slave->controller)
1490         send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1491       else
1492         send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1493                                  "Could not connect to delegated controller");
1494       GNUNET_SERVER_receive_done (client, GNUNET_OK);
1495       return;
1496     }
1497     lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1498     lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1499     GNUNET_SERVER_client_keep (client);
1500     lcc->client = client;
1501     slave->lcc = lcc;
1502     slave->controller_proc =
1503         GNUNET_TESTBED_controller_start (master_context->master_ip,
1504                                          host_list[slave->host_id], cfg,
1505                                          &slave_status_callback, slave);
1506     GNUNET_CONFIGURATION_destroy (cfg);
1507     new_route = GNUNET_malloc (sizeof (struct Route));
1508     new_route->dest = delegated_host_id;
1509     new_route->thru = master_context->host_id;
1510     route_list_add (new_route);
1511     return;
1512   }
1513
1514   /* Route the request */
1515   if (slave_host_id >= route_list_size)
1516   {
1517     LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1518     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1519     return;
1520   }
1521   lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1522   lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1523   lcfq->lcf->delegated_host_id = delegated_host_id;
1524   lcfq->lcf->slave_host_id = slave_host_id;
1525   route = find_dest_route (slave_host_id);
1526   GNUNET_assert (NULL != route);        /* because we add routes carefully */
1527   GNUNET_assert (route->dest < slave_list_size);
1528   GNUNET_assert (NULL != slave_list[route->dest]);
1529   lcfq->lcf->state = INIT;
1530   lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1531   lcfq->lcf->gateway = slave_list[route->dest];
1532   lcfq->lcf->msg = GNUNET_malloc (msize);
1533   (void) memcpy (lcfq->lcf->msg, msg, msize);
1534   GNUNET_SERVER_client_keep (client);
1535   lcfq->lcf->client = client;
1536   if (NULL == lcfq_head)
1537   {
1538     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1539     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1540     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1541   }
1542   else
1543     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1544   /* FIXME: Adding a new route should happen after the controllers are linked
1545    * successfully */
1546   if (1 != msg->is_subordinate)
1547   {
1548     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1549     return;
1550   }
1551   if ((delegated_host_id < route_list_size)
1552       && (NULL != route_list[delegated_host_id]))
1553   {
1554     GNUNET_break_op (0);        /* Are you trying to link delegated host twice
1555                                    with is subordinate flag set to GNUNET_YES? */
1556     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1557     return;
1558   }
1559   new_route = GNUNET_malloc (sizeof (struct Route));
1560   new_route->dest = delegated_host_id;
1561   new_route->thru = route->dest;
1562   route_list_add (new_route);
1563   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1564 }
1565
1566
1567 /**
1568  * The task to be executed if the forwarded peer create operation has been
1569  * timed out
1570  *
1571  * @param cls the FowardedOperationContext
1572  * @param tc the TaskContext from the scheduler
1573  */
1574 static void
1575 peer_create_forward_timeout (void *cls,
1576                              const struct GNUNET_SCHEDULER_TaskContext *tc)
1577 {
1578   struct ForwardedOperationContext *fopc = cls;
1579
1580   GNUNET_free (fopc->cls);
1581   forwarded_operation_timeout (fopc, tc);
1582 }
1583
1584
1585 /**
1586  * Callback to be called when forwarded peer create operation is successfull. We
1587  * have to relay the reply msg back to the client
1588  *
1589  * @param cls ForwardedOperationContext
1590  * @param msg the peer create success message
1591  */
1592 static void
1593 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1594 {
1595   struct ForwardedOperationContext *fopc = cls;
1596   struct Peer *remote_peer;
1597
1598   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1599   {
1600     GNUNET_assert (NULL != fopc->cls);
1601     remote_peer = fopc->cls;
1602     peer_list_add (remote_peer);
1603   }
1604   forwarded_operation_reply_relay (fopc, msg);
1605 }
1606
1607
1608 /**
1609  * Function to destroy a peer
1610  *
1611  * @param peer the peer structure to destroy
1612  */
1613 static void
1614 destroy_peer (struct Peer *peer)
1615 {
1616   GNUNET_break (0 == peer->reference_cnt);
1617   if (GNUNET_YES == peer->is_remote)
1618   {
1619     peer_list_remove (peer);
1620     GNUNET_free (peer);
1621     return;
1622   }
1623   if (GNUNET_YES == peer->details.local.is_running)
1624   {
1625     GNUNET_TESTING_peer_stop (peer->details.local.peer);
1626     peer->details.local.is_running = GNUNET_NO;
1627   }
1628   GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1629   GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1630   peer_list_remove (peer);
1631   GNUNET_free (peer);
1632 }
1633
1634
1635 /**
1636  * Callback to be called when forwarded peer destroy operation is successfull. We
1637  * have to relay the reply msg back to the client
1638  *
1639  * @param cls ForwardedOperationContext
1640  * @param msg the peer create success message
1641  */
1642 static void
1643 peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1644 {
1645   struct ForwardedOperationContext *fopc = cls;
1646   struct Peer *remote_peer;
1647
1648   if (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS == ntohs (msg->type))
1649   {
1650     remote_peer = fopc->cls;
1651     GNUNET_assert (NULL != remote_peer);
1652     remote_peer->destroy_flag = GNUNET_YES;
1653     if (0 == remote_peer->reference_cnt)
1654       destroy_peer (remote_peer);
1655   }
1656   forwarded_operation_reply_relay (fopc, msg);
1657 }
1658
1659
1660
1661 /**
1662  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1663  *
1664  * @param cls NULL
1665  * @param client identification of the client
1666  * @param message the actual message
1667  */
1668 static void
1669 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1670                     const struct GNUNET_MessageHeader *message)
1671 {
1672   const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1673   struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1674   struct GNUNET_CONFIGURATION_Handle *cfg;
1675   struct ForwardedOperationContext *fo_ctxt;
1676   struct Route *route;
1677   struct Peer *peer;
1678   char *config;
1679   size_t dest_size;
1680   int ret;
1681   uint32_t config_size;
1682   uint32_t host_id;
1683   uint32_t peer_id;
1684   uint16_t msize;
1685
1686
1687   msize = ntohs (message->size);
1688   if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1689   {
1690     GNUNET_break (0);           /* We need configuration */
1691     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1692     return;
1693   }
1694   msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1695   host_id = ntohl (msg->host_id);
1696   peer_id = ntohl (msg->peer_id);
1697   if (UINT32_MAX == peer_id)
1698   {
1699     send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1700                              "Cannot create peer with given ID");
1701     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1702     return;
1703   }
1704   if (host_id == master_context->host_id)
1705   {
1706     char *emsg;
1707
1708     /* We are responsible for this peer */
1709     msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1710     config_size = ntohl (msg->config_size);
1711     config = GNUNET_malloc (config_size);
1712     dest_size = config_size;
1713     if (Z_OK !=
1714         (ret =
1715          uncompress ((Bytef *) config, (uLongf *) & dest_size,
1716                      (const Bytef *) &msg[1], (uLong) msize)))
1717     {
1718       GNUNET_break (0);         /* uncompression error */
1719       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1720       return;
1721     }
1722     if (config_size != dest_size)
1723     {
1724       GNUNET_break (0);         /* Uncompressed config size mismatch */
1725       GNUNET_free (config);
1726       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1727       return;
1728     }
1729     cfg = GNUNET_CONFIGURATION_create ();
1730     if (GNUNET_OK !=
1731         GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1732     {
1733       GNUNET_break (0);         /* Configuration parsing error */
1734       GNUNET_free (config);
1735       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1736       return;
1737     }
1738     GNUNET_free (config);
1739     GNUNET_CONFIGURATION_set_value_number (cfg,
1740                                            "TESTBED",
1741                                            "PEERID",
1742                                            (unsigned long long) peer_id);
1743     peer = GNUNET_malloc (sizeof (struct Peer));
1744     peer->is_remote = GNUNET_NO;
1745     peer->details.local.cfg = cfg;
1746     peer->id = peer_id;
1747     LOG_DEBUG ("Creating peer with id: %u\n", 
1748                (unsigned int) peer->id);
1749     peer->details.local.peer =
1750         GNUNET_TESTING_peer_configure (master_context->system,
1751                                        peer->details.local.cfg, peer->id,
1752                                        NULL /* Peer id */ ,
1753                                        &emsg);
1754     if (NULL == peer->details.local.peer)
1755     {
1756       LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1757       GNUNET_free (emsg);
1758       GNUNET_free (peer);
1759       GNUNET_break (0);
1760       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1761       return;
1762     }
1763     peer->details.local.is_running = GNUNET_NO;
1764     peer_list_add (peer);
1765     reply =
1766         GNUNET_malloc (sizeof
1767                        (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1768     reply->header.size =
1769         htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1770     reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1771     reply->peer_id = msg->peer_id;
1772     reply->operation_id = msg->operation_id;
1773     queue_message (client, &reply->header);
1774     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1775     return;
1776   }
1777
1778   /* Forward peer create request */
1779   route = find_dest_route (host_id);
1780   if (NULL == route)
1781   {
1782     GNUNET_break (0);
1783     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1784     return;
1785   }
1786
1787   peer = GNUNET_malloc (sizeof (struct Peer));
1788   peer->is_remote = GNUNET_YES;
1789   peer->id = peer_id;
1790   peer->details.remote.slave = slave_list[route->dest];
1791   peer->details.remote.remote_host_id = host_id;
1792   fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1793   GNUNET_SERVER_client_keep (client);
1794   fo_ctxt->client = client;
1795   fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1796   fo_ctxt->cls = peer; //slave_list[route->dest]->controller;
1797   fo_ctxt->type = OP_PEER_CREATE;
1798   fo_ctxt->opc =
1799       GNUNET_TESTBED_forward_operation_msg_ (slave_list [route->dest]->controller,
1800                                              fo_ctxt->operation_id,
1801                                              &msg->header,
1802                                              peer_create_success_cb, fo_ctxt);
1803   fo_ctxt->timeout_task =
1804       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1805                                     fo_ctxt);
1806   GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
1807   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1808 }
1809
1810
1811 /**
1812  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1813  *
1814  * @param cls NULL
1815  * @param client identification of the client
1816  * @param message the actual message
1817  */
1818 static void
1819 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1820                      const struct GNUNET_MessageHeader *message)
1821 {
1822   const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1823   struct ForwardedOperationContext *fopc;
1824   struct Peer *peer;
1825   uint32_t peer_id;
1826
1827   msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1828   peer_id = ntohl (msg->peer_id);
1829   LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1830              peer_id, GNUNET_ntohll (msg->operation_id));
1831   if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1832   {
1833     LOG (GNUNET_ERROR_TYPE_ERROR,
1834          "Asked to destroy a non existent peer with id: %u\n", peer_id);
1835     send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1836                              "Peer doesn't exist");
1837     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1838     return;
1839   }
1840   peer = peer_list[peer_id];
1841   if (GNUNET_YES == peer->is_remote)
1842   {
1843     /* Forward the destory message to sub controller */
1844     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1845     GNUNET_SERVER_client_keep (client);
1846     fopc->client = client;
1847     fopc->cls = peer;
1848     fopc->type = OP_PEER_DESTROY;
1849     fopc->operation_id = GNUNET_ntohll (msg->operation_id);    
1850     fopc->opc = 
1851         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
1852                                                fopc->operation_id, &msg->header,
1853                                                &peer_destroy_success_cb,
1854                                                fopc);
1855     fopc->timeout_task =
1856         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1857                                       fopc);
1858     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1859     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1860     return;
1861   }
1862   peer->destroy_flag = GNUNET_YES;
1863   if (0 == peer->reference_cnt)
1864     destroy_peer (peer);
1865   else
1866     LOG (GNUNET_ERROR_TYPE_DEBUG,
1867          "Delaying peer destroy as peer is currently in use\n");
1868   send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1869   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1870 }
1871
1872
1873 /**
1874  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1875  *
1876  * @param cls NULL
1877  * @param client identification of the client
1878  * @param message the actual message
1879  */
1880 static void
1881 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1882                    const struct GNUNET_MessageHeader *message)
1883 {
1884   const struct GNUNET_TESTBED_PeerStartMessage *msg;
1885   struct GNUNET_TESTBED_PeerEventMessage *reply;
1886   struct ForwardedOperationContext *fopc;
1887   struct Peer *peer;
1888   uint32_t peer_id;
1889
1890   msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1891   peer_id = ntohl (msg->peer_id);
1892   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1893   {
1894     GNUNET_break (0);
1895     LOG (GNUNET_ERROR_TYPE_ERROR,
1896          "Asked to start a non existent peer with id: %u\n", peer_id);
1897     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1898     return;
1899   }
1900   peer = peer_list[peer_id];
1901   if (GNUNET_YES == peer->is_remote)
1902   {
1903     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1904     GNUNET_SERVER_client_keep (client);
1905     fopc->client = client;
1906     fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1907     fopc->type = OP_PEER_START;
1908     fopc->opc =
1909         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
1910                                                fopc->operation_id, &msg->header,
1911                                                &forwarded_operation_reply_relay,
1912                                                fopc);
1913     fopc->timeout_task =
1914         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1915                                       fopc);
1916     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1917     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1918     return;
1919   }
1920   if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1921   {
1922     send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1923                              "Failed to start");
1924     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1925     return;
1926   }
1927   peer->details.local.is_running = GNUNET_YES;
1928   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1929   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1930   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1931   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1932   reply->host_id = htonl (master_context->host_id);
1933   reply->peer_id = msg->peer_id;
1934   reply->operation_id = msg->operation_id;
1935   queue_message (client, &reply->header);
1936   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1937 }
1938
1939
1940 /**
1941  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1942  *
1943  * @param cls NULL
1944  * @param client identification of the client
1945  * @param message the actual message
1946  */
1947 static void
1948 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1949                   const struct GNUNET_MessageHeader *message)
1950 {
1951   const struct GNUNET_TESTBED_PeerStopMessage *msg;
1952   struct GNUNET_TESTBED_PeerEventMessage *reply;
1953   struct ForwardedOperationContext *fopc;
1954   struct Peer *peer;
1955   uint32_t peer_id;
1956
1957   msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1958   peer_id = ntohl (msg->peer_id);
1959   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PEER_STOP for peer %u\n", peer_id);
1960   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1961   {
1962     send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1963                              "Peer not found");
1964     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1965     return;
1966   }
1967   peer = peer_list[peer_id];
1968   if (GNUNET_YES == peer->is_remote)
1969   {
1970     LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarding PEER_STOP for peer %u\n", peer_id);
1971     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1972     GNUNET_SERVER_client_keep (client);
1973     fopc->client = client;
1974     fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1975     fopc->type = OP_PEER_STOP;
1976     fopc->opc =
1977         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
1978                                                fopc->operation_id, &msg->header,
1979                                                &forwarded_operation_reply_relay,
1980                                                fopc);
1981     fopc->timeout_task =
1982         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1983                                       fopc);
1984     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1985     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1986     return;
1987   }
1988   if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1989   {
1990     LOG (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %u failed\n", peer_id);
1991     send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1992                              "Peer not running");
1993     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1994     return;
1995   }
1996   LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %u successfully stopped\n", peer_id);
1997   peer->details.local.is_running = GNUNET_NO;
1998   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1999   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2000   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2001   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2002   reply->host_id = htonl (master_context->host_id);
2003   reply->peer_id = msg->peer_id;
2004   reply->operation_id = msg->operation_id;
2005   queue_message (client, &reply->header);
2006   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2007 }
2008
2009
2010 /**
2011  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2012  *
2013  * @param cls NULL
2014  * @param client identification of the client
2015  * @param message the actual message
2016  */
2017 static void
2018 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2019                         const struct GNUNET_MessageHeader *message)
2020 {
2021   const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2022   struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2023   struct Peer *peer;
2024   char *config;
2025   char *xconfig;
2026   size_t c_size;
2027   size_t xc_size;
2028   uint32_t peer_id;
2029   uint16_t msize;
2030
2031   msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2032   peer_id = ntohl (msg->peer_id);
2033   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2034   {
2035     send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2036                              "Peer not found");
2037     GNUNET_SERVER_receive_done (client, GNUNET_OK);
2038     return;
2039   }
2040   peer = peer_list[peer_id];
2041   if (GNUNET_YES == peer->is_remote)
2042   {
2043     struct ForwardedOperationContext *fopc;
2044     
2045     LOG_DEBUG ("Forwarding PEER_GET_CONFIG for peer: %u\n", peer_id);
2046     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2047     GNUNET_SERVER_client_keep (client);
2048     fopc->client = client;
2049     fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2050     fopc->type = OP_PEER_INFO;
2051     fopc->opc =
2052         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2053                                                fopc->operation_id, &msg->header,
2054                                                &forwarded_operation_reply_relay,
2055                                                fopc);
2056     fopc->timeout_task =
2057         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2058                                       fopc);
2059     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc); 
2060     GNUNET_SERVER_receive_done (client, GNUNET_OK);
2061     return;
2062   }
2063   LOG_DEBUG ("Received PEER_GET_CONFIG for peer: %u\n", peer_id);
2064   config =
2065       GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2066                                       &c_size);
2067   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2068   GNUNET_free (config);
2069   msize =
2070       xc_size +
2071       sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2072   reply = GNUNET_realloc (xconfig, msize);
2073   (void) memmove (&reply[1], reply, xc_size);
2074   reply->header.size = htons (msize);
2075   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2076   reply->peer_id = msg->peer_id;
2077   reply->operation_id = msg->operation_id;
2078   GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2079                                     &reply->peer_identity);
2080   reply->config_size = htons ((uint16_t) c_size);
2081   queue_message (client, &reply->header);
2082   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2083 }
2084
2085
2086 /**
2087  * Cleanup overlay connect context structure
2088  *
2089  * @param occ the overlay connect context
2090  */
2091 static void
2092 cleanup_occ (struct OverlayConnectContext *occ)
2093 {
2094   LOG_DEBUG ("0x%llx: Cleaning up occ\n", occ->op_id);
2095   GNUNET_free_non_null (occ->emsg);
2096   GNUNET_free_non_null (occ->hello);
2097   GNUNET_SERVER_client_drop (occ->client);
2098   if (NULL != occ->opc)
2099     GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2100   if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2101     GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2102   if (GNUNET_SCHEDULER_NO_TASK != occ->cleanup_task)
2103     GNUNET_SCHEDULER_cancel (occ->cleanup_task);
2104   if (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task)
2105     GNUNET_SCHEDULER_cancel (occ->timeout_task);
2106   if (NULL != occ->ch)
2107   {
2108     GNUNET_CORE_disconnect (occ->ch);
2109     occ->peer->reference_cnt--;
2110   }
2111   if (NULL != occ->ghh)
2112     GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2113   if (NULL != occ->ohh)
2114     GNUNET_TRANSPORT_offer_hello_cancel (occ->ohh);
2115   if (GNUNET_SCHEDULER_NO_TASK != occ->tcc.task)
2116     GNUNET_SCHEDULER_cancel (occ->tcc.task);
2117   if (NULL != occ->tcc.tch)
2118     GNUNET_TRANSPORT_try_connect_cancel (occ->tcc.tch);
2119   if (NULL != occ->p1th)
2120   {
2121     GNUNET_TRANSPORT_disconnect (occ->p1th);
2122     occ->peer->reference_cnt--;
2123   }
2124   if (NULL != occ->tcc.th)
2125   {
2126     GNUNET_TRANSPORT_disconnect (occ->tcc.th);
2127     peer_list[occ->other_peer_id]->reference_cnt--;
2128   }
2129   if ((GNUNET_YES == occ->peer->destroy_flag)
2130       && (0 == occ->peer->reference_cnt))
2131     destroy_peer (occ->peer);
2132   if ((NULL == occ->peer2_controller)
2133       && (GNUNET_YES == peer_list[occ->other_peer_id]->destroy_flag)
2134         && (0 == peer_list[occ->other_peer_id]->reference_cnt))
2135       destroy_peer (peer_list[occ->other_peer_id]);  
2136   GNUNET_CONTAINER_DLL_remove (occq_head, occq_tail, occ);
2137   GNUNET_free (occ);
2138 }
2139
2140
2141 /**
2142  * Task for cleaing up overlay connect context structure
2143  *
2144  * @param cls the overlay connect context
2145  * @param tc the task context
2146  */
2147 static void
2148 do_cleanup_occ (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2149 {
2150   struct OverlayConnectContext *occ = cls;
2151   
2152   occ->cleanup_task = GNUNET_SCHEDULER_NO_TASK;
2153   cleanup_occ (occ);
2154 }
2155
2156
2157 /**
2158  * Task which will be run when overlay connect request has been timed out
2159  *
2160  * @param cls the OverlayConnectContext
2161  * @param tc the TaskContext
2162  */
2163 static void
2164 timeout_overlay_connect (void *cls,
2165                          const struct GNUNET_SCHEDULER_TaskContext *tc)
2166 {
2167   struct OverlayConnectContext *occ = cls;
2168
2169   occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2170   LOG (GNUNET_ERROR_TYPE_WARNING,
2171        "0x%llx: Timeout while connecting peers %u and %u\n",
2172        occ->op_id, occ->peer_id, occ->other_peer_id);
2173   send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2174   cleanup_occ (occ);
2175 }
2176
2177
2178
2179 /**
2180  * Function called to notify transport users that another
2181  * peer connected to us.
2182  *
2183  * @param cls closure
2184  * @param new_peer the peer that connected
2185  * @param ats performance data
2186  * @param ats_count number of entries in ats (excluding 0-termination)
2187  */
2188 static void
2189 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2190                         const struct GNUNET_ATS_Information *ats,
2191                         unsigned int ats_count)
2192 {
2193   struct OverlayConnectContext *occ = cls;
2194   struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2195   char *new_peer_str;
2196   char *other_peer_str;
2197
2198   //LOG_DEBUG ("Overlay connect notify\n");
2199   if (0 ==
2200       memcmp (new_peer, &occ->peer_identity,
2201               sizeof (struct GNUNET_PeerIdentity)))
2202     return;
2203   new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2204   other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2205   if (0 !=
2206       memcmp (new_peer, &occ->other_peer_identity,
2207               sizeof (struct GNUNET_PeerIdentity)))
2208   {
2209     /* LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n", */
2210     /*         new_peer_str, other_peer_str); */
2211     GNUNET_free (new_peer_str);
2212     GNUNET_free (other_peer_str);
2213     return;
2214   }
2215   GNUNET_free (new_peer_str);
2216   LOG_DEBUG ("0x%llx: Peer %4s connected to peer %4s\n", occ->op_id,
2217              other_peer_str, GNUNET_i2s (&occ->peer_identity));
2218   GNUNET_free (other_peer_str);
2219   if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2220   {
2221     GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2222     occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2223   }
2224   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2225   GNUNET_SCHEDULER_cancel (occ->timeout_task);
2226   occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2227   if (GNUNET_SCHEDULER_NO_TASK != occ->tcc.task)
2228   {
2229     GNUNET_SCHEDULER_cancel (occ->tcc.task);
2230     occ->tcc.task = GNUNET_SCHEDULER_NO_TASK;
2231   }
2232   GNUNET_free_non_null (occ->emsg);
2233   occ->emsg = NULL;
2234   LOG_DEBUG ("0x%llx: Peers connected - Sending overlay connect success\n",
2235              occ->op_id);
2236   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2237   msg->header.size =
2238       htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2239   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2240   msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2241   msg->peer1 = htonl (occ->peer_id);
2242   msg->peer2 = htonl (occ->other_peer_id);
2243   msg->operation_id = GNUNET_htonll (occ->op_id);
2244   queue_message (occ->client, &msg->header);
2245   occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
2246   //cleanup_occ (occ);
2247 }
2248
2249
2250 /**
2251  * Task to ask transport of a peer to connect to another peer
2252  *
2253  * @param cls the TryConnectContext
2254  * @param tc the scheduler task context
2255  */
2256 static void
2257 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2258
2259
2260 /**
2261  * Callback to be called with result of the try connect request.
2262  *
2263  * @param cls the overlay connect context
2264  * @param result GNUNET_OK if message was transmitted to transport service
2265  *               GNUNET_SYSERR if message was not transmitted to transport service
2266  */
2267 static void 
2268 try_connect_cb (void *cls, const int result)
2269 {
2270   struct TryConnectContext *tcc = cls;
2271
2272   tcc->tch = NULL;
2273   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == tcc->task);
2274   tcc->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2275                                             (GNUNET_TIME_UNIT_MILLISECONDS,
2276                                              500 + pow(2, ++tcc->retries)),
2277                                             &try_connect_task, tcc);
2278 }
2279
2280
2281 /**
2282  * Task to ask transport of a peer to connect to another peer
2283  *
2284  * @param cls the TryConnectContext
2285  * @param tc the scheduler task context
2286  */
2287 static void
2288 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2289 {
2290   struct TryConnectContext *tcc = cls;
2291
2292   tcc->task = GNUNET_SCHEDULER_NO_TASK;
2293   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2294     return;  
2295   GNUNET_assert (NULL == tcc->tch);
2296   GNUNET_assert (NULL != tcc->pid);
2297   GNUNET_assert (NULL != tcc->th);
2298   LOG_DEBUG ("0x%llx: Trail %u to connect to peer %s\n", tcc->op_id, tcc->retries,
2299              GNUNET_i2s(tcc->pid));
2300   tcc->tch = GNUNET_TRANSPORT_try_connect (tcc->th, tcc->pid, &try_connect_cb, tcc);
2301 }
2302
2303
2304 /**
2305  * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2306  * peer 1.
2307  *
2308  * @param cls the OverlayConnectContext
2309  * @param tc the TaskContext from scheduler
2310  */
2311 static void
2312 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2313
2314
2315 /**
2316  * Task that is run when hello has been sent
2317  *
2318  * @param cls the overlay connect context
2319  * @param tc the scheduler task context; if tc->reason =
2320  *          GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
2321  *          GNUNET_SCHEDULER_REASON_READ_READY is succeeded
2322  */
2323 static void
2324 occ_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2325 {
2326   struct OverlayConnectContext *occ = cls;
2327
2328   occ->ohh = NULL;
2329   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == occ->send_hello_task);
2330   if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
2331   {
2332     GNUNET_free_non_null (occ->emsg);
2333     GNUNET_asprintf (&occ->emsg,
2334                      "0x%llx: Timeout while offering HELLO to other peer",
2335                      occ->op_id);
2336     occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2337     return;
2338   }
2339   if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
2340     return;
2341   GNUNET_free_non_null (occ->emsg);
2342   GNUNET_asprintf (&occ->emsg, "0x%llx: Timeout while try connect",
2343                    occ->op_id);
2344   occ->tcc.pid =  &occ->peer_identity;
2345   occ->tcc.op_id = occ->op_id;
2346   occ->tcc.task = GNUNET_SCHEDULER_add_now (&try_connect_task, &occ->tcc);
2347 }
2348
2349
2350 /**
2351  * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2352  * peer 1.
2353  *
2354  * @param cls the OverlayConnectContext
2355  * @param tc the TaskContext from scheduler
2356  */
2357 static void
2358 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2359 {
2360   struct OverlayConnectContext *occ = cls;
2361   char *other_peer_str;
2362
2363   occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2364   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2365     return;
2366   GNUNET_assert (NULL != occ->hello);
2367   other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2368   if (NULL != occ->peer2_controller)
2369   {
2370     struct GNUNET_TESTBED_RequestConnectMessage *msg;
2371     uint16_t msize;
2372     uint16_t hello_size;
2373
2374     LOG_DEBUG ("0x%llx: Offering HELLO of %s (size: %u) to %s via Remote "
2375                "Overlay Request\n",
2376                occ->op_id,
2377                GNUNET_i2s (&occ->peer_identity), 
2378                ntohs (occ->hello->size),
2379                other_peer_str);
2380     hello_size = ntohs (occ->hello->size);
2381     msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2382     msg = GNUNET_malloc (msize);
2383     msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2384     msg->header.size = htons (msize);
2385     msg->peer = htonl (occ->other_peer_id);
2386     msg->operation_id = GNUNET_htonll (occ->op_id);
2387     (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2388                    sizeof (struct GNUNET_PeerIdentity));
2389     memcpy (msg->hello, occ->hello, hello_size);
2390     GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2391   }
2392   else
2393   {
2394     LOG_DEBUG ("0x%llx: Offering HELLO of %s to %s\n",
2395                occ->op_id,
2396                GNUNET_i2s (&occ->peer_identity), other_peer_str);
2397     occ->ohh = GNUNET_TRANSPORT_offer_hello (occ->tcc.th,
2398                                              occ->hello,
2399                                              occ_hello_sent_cb,
2400                                              occ);
2401     if (NULL == occ->ohh)
2402     {
2403       GNUNET_break (0);
2404       occ->send_hello_task =
2405           GNUNET_SCHEDULER_add_delayed
2406           (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2407                                           100 + GNUNET_CRYPTO_random_u32
2408                                           (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
2409            &send_hello, occ);
2410     }
2411   }
2412   GNUNET_free (other_peer_str);  
2413 }
2414
2415
2416 /**
2417  * Connects to the transport of the other peer if it is a local peer and
2418  * schedules the send hello task
2419  *
2420  * @param occ the overlay connect context
2421  */
2422 static void
2423 p2_transport_connect (struct OverlayConnectContext *occ)
2424 {
2425   GNUNET_assert (NULL == occ->emsg);
2426   GNUNET_assert (NULL != occ->hello);
2427   GNUNET_assert (NULL == occ->ghh);
2428   GNUNET_assert (NULL == occ->p1th);
2429   if (NULL == occ->peer2_controller)
2430   {
2431     peer_list[occ->other_peer_id]->reference_cnt++;
2432     occ->tcc.th =
2433         GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
2434                                   &occ->other_peer_identity, NULL, NULL, NULL,
2435                                   NULL);
2436     if (NULL == occ->tcc.th)
2437     {
2438       GNUNET_asprintf (&occ->emsg, "0x%llx: Cannot connect to TRANSPORT of %s",
2439                        occ->op_id, GNUNET_i2s (&occ->other_peer_identity));
2440       GNUNET_SCHEDULER_cancel (occ->timeout_task);
2441       occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2442       return;
2443     }
2444   }
2445   GNUNET_asprintf (&occ->emsg, "0x%llx: Timeout while offering HELLO to %s",
2446                    occ->op_id, GNUNET_i2s (&occ->other_peer_identity));
2447   occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2448 }
2449
2450
2451 /**
2452  * Test for checking whether HELLO message is empty
2453  *
2454  * @param cls empty flag to set
2455  * @param address the HELLO
2456  * @param expiration expiration of the HELLO
2457  * @return
2458  */
2459 static int
2460 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2461               struct GNUNET_TIME_Absolute expiration)
2462 {
2463   int *empty = cls;
2464
2465   *empty = GNUNET_NO;
2466   return GNUNET_OK;
2467 }
2468
2469
2470 /**
2471  * Function called whenever there is an update to the HELLO of peers in the
2472  * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2473  * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2474  *
2475  * @param cls closure
2476  * @param hello our updated HELLO
2477  */
2478 static void
2479 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2480 {
2481   struct OverlayConnectContext *occ = cls;
2482   int empty;
2483   uint16_t msize;
2484
2485   msize = ntohs (hello->size);
2486   empty = GNUNET_YES;
2487   (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2488                                          hello, GNUNET_NO, &test_address,
2489                                          &empty);
2490   if (GNUNET_YES == empty)
2491   {
2492     LOG_DEBUG ("0x%llx: HELLO of %s is empty\n",
2493                occ->op_id, GNUNET_i2s (&occ->peer_identity));
2494     return;
2495   }
2496   LOG_DEBUG ("0x%llx: Received HELLO of %s\n",
2497              occ->op_id, GNUNET_i2s (&occ->peer_identity));
2498   occ->hello = GNUNET_malloc (msize);
2499   hello_cache_add (&occ->peer_identity, hello);
2500   memcpy (occ->hello, hello, msize);
2501   GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2502   occ->ghh = NULL;
2503   GNUNET_TRANSPORT_disconnect (occ->p1th);
2504   occ->p1th = NULL;
2505   occ->peer->reference_cnt--;
2506   GNUNET_free_non_null (occ->emsg);
2507   occ->emsg = NULL;
2508   p2_transport_connect (occ);
2509 }
2510
2511
2512 /**
2513  * Function called after GNUNET_CORE_connect has succeeded (or failed
2514  * for good).  Note that the private key of the peer is intentionally
2515  * not exposed here; if you need it, your process should try to read
2516  * the private key file directly (which should work if you are
2517  * authorized...).
2518  *
2519  * @param cls closure
2520  * @param server handle to the server, NULL if we failed
2521  * @param my_identity ID of this peer, NULL if we failed
2522  */
2523 static void
2524 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2525                  const struct GNUNET_PeerIdentity *my_identity)
2526 {
2527   struct OverlayConnectContext *occ = cls;
2528   const struct GNUNET_MessageHeader *hello;
2529
2530   GNUNET_free_non_null (occ->emsg);
2531   (void) GNUNET_asprintf (&occ->emsg,
2532                           "0x%llx: Failed to connect to CORE of peer with"
2533                           "id: %u", occ->op_id, occ->peer_id);
2534   if ((NULL == server) || (NULL == my_identity))
2535     goto error_return;
2536   GNUNET_free (occ->emsg);
2537   occ->ch = server;
2538   occ->emsg = NULL;
2539   memcpy (&occ->peer_identity, my_identity,
2540           sizeof (struct GNUNET_PeerIdentity));
2541   LOG_DEBUG ("0x%llx: Acquiring HELLO of peer %s\n",
2542              occ->op_id, GNUNET_i2s (&occ->peer_identity));
2543   /* Lookup for HELLO in hello cache */
2544   if (NULL != (hello = hello_cache_lookup (&occ->peer_identity)))
2545   {
2546     LOG_DEBUG ("0x%llx: HELLO of peer %s found in cache\n",
2547                occ->op_id, GNUNET_i2s (&occ->peer_identity));
2548     occ->hello = GNUNET_copy_message (hello);
2549     p2_transport_connect (occ);
2550     return;
2551   }
2552   occ->peer->reference_cnt++;
2553   occ->p1th =
2554       GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2555                                 &occ->peer_identity, NULL, NULL, NULL, NULL);
2556   if (NULL == occ->p1th)
2557   {
2558     GNUNET_asprintf (&occ->emsg,
2559                      "0x%llx: Cannot connect to TRANSPORT of peer %4s",
2560                      occ->op_id, GNUNET_i2s (&occ->peer_identity));
2561     goto error_return;
2562   }  
2563   GNUNET_asprintf (&occ->emsg,
2564                    "0x%llx: Timeout while acquiring HELLO of peer %4s",
2565                    occ->op_id, GNUNET_i2s (&occ->peer_identity));
2566   occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2567   return;
2568   
2569  error_return:
2570   GNUNET_SCHEDULER_cancel (occ->timeout_task);
2571   occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2572   return;
2573 }
2574
2575
2576 /**
2577  * Callback to be called when forwarded get peer config operation as part of
2578  * overlay connect is successfull. Connection to Peer 1's core is made and is
2579  * checked for new connection from peer 2
2580  *
2581  * @param cls ForwardedOperationContext
2582  * @param msg the peer create success message
2583  */
2584 static void
2585 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2586 {
2587   struct OverlayConnectContext *occ = cls;
2588   const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2589   const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2590     {NULL, 0, 0}
2591   };
2592
2593   occ->opc = NULL;
2594   if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2595     goto error_return;
2596   cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2597       msg;
2598   memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2599           sizeof (struct GNUNET_PeerIdentity));
2600   GNUNET_free_non_null (occ->emsg);
2601   GNUNET_asprintf (&occ->emsg, "0x%llx: Timeout while connecting to CORE",
2602                    occ->op_id);
2603   occ->peer->reference_cnt++;
2604   occ->ch =
2605       GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2606                            &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2607                            GNUNET_NO, no_handlers);
2608   if (NULL == occ->ch)
2609     goto error_return;
2610   return;
2611
2612  error_return:
2613   GNUNET_SCHEDULER_cancel (occ->timeout_task);
2614   occ->timeout_task = 
2615       GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2616 }
2617
2618
2619 /**
2620  * Callback which will be called to after a host registration succeeded or failed
2621  *
2622  * @param cls the RegisteredHostContext
2623  * @param emsg the error message; NULL if host registration is successful
2624  */
2625 static void 
2626 registeredhost_registration_completion (void *cls, const char *emsg)
2627 {
2628   struct RegisteredHostContext *rhc = cls;
2629   struct GNUNET_CONFIGURATION_Handle *cfg;
2630   uint32_t peer2_host_id;
2631
2632   /* if (NULL != rhc->focc_dll_head) */
2633   /*   process_next_focc (rhc); */
2634   peer2_host_id = GNUNET_TESTBED_host_get_id_ (rhc->reg_host);
2635   GNUNET_assert (RHC_INIT == rhc->state);
2636   GNUNET_assert (NULL == rhc->sub_op);
2637   if ((NULL == rhc->gateway2)
2638       || ((peer2_host_id < slave_list_size) /* Check if we have the needed config */
2639           && (NULL != slave_list[peer2_host_id])))
2640   {
2641     rhc->state = RHC_LINK;
2642     cfg = (NULL == rhc->gateway2) ? our_config : slave_list[peer2_host_id]->cfg;
2643     rhc->sub_op =
2644         GNUNET_TESTBED_controller_link (rhc,
2645                                         rhc->gateway->controller,
2646                                         rhc->reg_host,
2647                                         rhc->host,
2648                                         cfg,
2649                                         GNUNET_NO);
2650     return;
2651   }
2652   rhc->state = RHC_GET_CFG;
2653   rhc->sub_op =  GNUNET_TESTBED_get_slave_config (rhc,
2654                                                   rhc->gateway2->controller,
2655                                                   rhc->reg_host);
2656 }
2657
2658
2659 /**
2660  * Iterator to match a registered host context
2661  *
2662  * @param cls pointer 2 pointer of RegisteredHostContext
2663  * @param key current key code
2664  * @param value value in the hash map
2665  * @return GNUNET_YES if we should continue to
2666  *         iterate,
2667  *         GNUNET_NO if not.
2668  */
2669 static int 
2670 reghost_match_iterator (void *cls,
2671                         const struct GNUNET_HashCode * key,
2672                         void *value)
2673 {
2674   struct RegisteredHostContext **rh = cls;
2675   struct RegisteredHostContext *rh_val = value;
2676
2677   if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
2678   {
2679     GNUNET_free (*rh);
2680     *rh = rh_val;
2681     return GNUNET_NO;
2682   }
2683   return GNUNET_YES;
2684 }
2685
2686
2687 /**
2688  * Function to generate the hashcode corresponding to a RegisteredHostContext
2689  *
2690  * @param reg_host the host which is being registered in RegisteredHostContext
2691  * @param host the host of the controller which has to connect to the above rhost
2692  * @return the hashcode
2693  */
2694 static struct GNUNET_HashCode
2695 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
2696             struct GNUNET_TESTBED_Host *host)
2697 {
2698   struct GNUNET_HashCode hash;
2699   uint32_t host_ids[2];
2700
2701   host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
2702   host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
2703   GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
2704   return hash;
2705 }
2706
2707
2708 /**
2709  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2710  *
2711  * @param cls NULL
2712  * @param client identification of the client
2713  * @param message the actual message
2714  */
2715 static void
2716 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2717                         const struct GNUNET_MessageHeader *message)
2718 {
2719   const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2720   const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2721     {NULL, 0, 0}
2722   };
2723   struct Peer *peer;
2724   struct OverlayConnectContext *occ;
2725   struct GNUNET_TESTBED_Controller *peer2_controller;
2726   uint64_t operation_id;
2727   uint32_t p1;
2728   uint32_t p2; 
2729   uint32_t peer2_host_id;
2730
2731   msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2732   p1 = ntohl (msg->peer1);
2733   p2 = ntohl (msg->peer2);
2734   peer2_host_id = ntohl (msg->peer2_host_id);
2735   GNUNET_assert (p1 < peer_list_size);
2736   GNUNET_assert (NULL != peer_list[p1]);
2737   peer = peer_list[p1];
2738   operation_id = GNUNET_ntohll (msg->operation_id);  
2739   LOG_DEBUG ("Received overlay connect for peers %u and %u with op id: 0x%llx\n",
2740              p1, p2, operation_id);
2741   if (GNUNET_YES == peer->is_remote)
2742   {
2743     struct ForwardedOperationContext *fopc;
2744     struct Route *route_to_peer2_host;
2745     struct Route *route_to_peer1_host;
2746
2747     LOG_DEBUG ("0x%llx: Forwarding overlay connect\n", operation_id);
2748     route_to_peer2_host = NULL;
2749     route_to_peer1_host = NULL;
2750     route_to_peer2_host = find_dest_route (peer2_host_id);
2751     if ((NULL != route_to_peer2_host) 
2752         || (peer2_host_id == master_context->host_id))
2753     {
2754       /* Peer 2 either below us OR with us */
2755       route_to_peer1_host = 
2756           find_dest_route (peer_list[p1]->details.remote.remote_host_id);
2757       /* Because we get this message only if we know where peer 1 is */
2758       GNUNET_assert (NULL != route_to_peer1_host);
2759       if ((peer2_host_id == master_context->host_id) 
2760           || (route_to_peer2_host->dest != route_to_peer1_host->dest))
2761       {
2762         /* Peer2 is either with us OR peer1 and peer2 can be reached through
2763            different gateways */
2764         struct GNUNET_HashCode hash;
2765         struct RegisteredHostContext *rhc;
2766         int skip_focc;
2767
2768         rhc = GNUNET_malloc (sizeof (struct RegisteredHostContext));
2769         if (NULL != route_to_peer2_host)
2770           rhc->reg_host = host_list[route_to_peer2_host->dest];
2771         else
2772           rhc->reg_host = host_list[master_context->host_id];
2773         rhc->host = host_list[route_to_peer1_host->dest];
2774         GNUNET_assert (NULL != rhc->reg_host);
2775         GNUNET_assert (NULL != rhc->host);
2776         rhc->gateway = peer->details.remote.slave;
2777         rhc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
2778             slave_list[route_to_peer2_host->dest];
2779         rhc->state = RHC_INIT;
2780         GNUNET_SERVER_client_keep (client);
2781         rhc->client = client;
2782         hash = hash_hosts (rhc->reg_host, rhc->host);
2783         skip_focc = GNUNET_NO;
2784         if ((GNUNET_NO == 
2785              GNUNET_CONTAINER_multihashmap_contains
2786              (peer->details.remote.slave->reghost_map, &hash))
2787             || (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple
2788                 (peer->details.remote.slave->reghost_map, &hash,
2789                  reghost_match_iterator, &rhc)))
2790         {
2791           /* create and add a new registerd host context */
2792           /* add the focc to its queue */
2793           GNUNET_CONTAINER_multihashmap_put
2794               (peer->details.remote.slave->reghost_map,
2795                &hash, rhc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2796           GNUNET_assert (NULL != host_list[peer2_host_id]);
2797           queue_host_registration (peer->details.remote.slave,
2798                                    registeredhost_registration_completion, rhc,
2799                                    host_list[peer2_host_id]);
2800         }
2801         else {
2802           /* rhc is now set to the existing one from the hash map by
2803              reghost_match_iterator() */
2804           /* if queue is empty then ignore creating focc and proceed with
2805              normal forwarding */                 
2806           if (RHC_OL_CONNECT == rhc->state)
2807             skip_focc = GNUNET_YES;
2808         }
2809         if (GNUNET_NO == skip_focc)
2810         {
2811           struct ForwardedOverlayConnectContext *focc;
2812
2813           focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
2814           focc->peer1 = p1;
2815           focc->peer2 = p2;
2816           focc->peer2_host_id = peer2_host_id;
2817           focc->orig_msg = GNUNET_copy_message (message);
2818           focc->operation_id = operation_id;
2819           GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
2820                                             rhc->focc_dll_tail,
2821                                             focc);
2822           GNUNET_SERVER_receive_done (client, GNUNET_OK);
2823           return;
2824         }
2825       }
2826     }
2827     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2828     GNUNET_SERVER_client_keep (client);
2829     fopc->client = client;
2830     fopc->operation_id = operation_id;
2831     fopc->type = OP_OVERLAY_CONNECT;
2832     fopc->opc = 
2833         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2834                                                operation_id, message,
2835                                                &forwarded_operation_reply_relay,
2836                                                fopc);
2837     fopc->timeout_task =
2838         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2839                                       fopc);
2840     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2841     GNUNET_SERVER_receive_done (client, GNUNET_OK);
2842     return;
2843   }
2844
2845   peer2_controller = NULL;
2846   if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
2847   {   
2848     if ((peer2_host_id >= slave_list_size)
2849         || (NULL ==slave_list[peer2_host_id]))
2850     {
2851       LOG (GNUNET_ERROR_TYPE_WARNING,
2852            "0x%llx: Configuration of peer2's controller missing for connecting peers"
2853            "%u and %u\n", operation_id, p1, p2);      
2854       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2855       return;
2856     }
2857     peer2_controller = slave_list[peer2_host_id]->controller;
2858     if (NULL == peer2_controller)
2859     {
2860       GNUNET_break (0);       /* What's going on? */
2861       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2862       return;
2863     }
2864   }
2865   else
2866   {
2867     if (GNUNET_YES == peer_list[p2]->is_remote)
2868       peer2_controller = peer_list[p2]->details.remote.slave->controller;
2869   }
2870   occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2871   GNUNET_CONTAINER_DLL_insert_tail (occq_head, occq_tail, occ);
2872   GNUNET_SERVER_client_keep (client);
2873   occ->client = client;
2874   occ->peer_id = p1;
2875   occ->other_peer_id = p2;
2876   occ->peer = peer_list[p1];
2877   occ->op_id = GNUNET_ntohll (msg->operation_id);
2878   occ->peer2_controller = peer2_controller;
2879   /* Get the identity of the second peer */
2880   if (NULL != occ->peer2_controller)
2881   {
2882     struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2883
2884     cmsg.header.size = 
2885         htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2886     cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2887     cmsg.peer_id = msg->peer2;
2888     cmsg.operation_id = msg->operation_id;
2889     occ->opc = 
2890         GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
2891                                                occ->op_id, &cmsg.header,
2892                                                &overlay_connect_get_config,
2893                                                occ);
2894     GNUNET_asprintf (&occ->emsg,
2895                      "0x%llx: Timeout while getting peer identity of peer "
2896                      "with id: %u", occ->op_id, occ->other_peer_id);
2897     occ->timeout_task =
2898         GNUNET_SCHEDULER_add_delayed (TIMEOUT,
2899                                       &timeout_overlay_connect, occ);
2900     GNUNET_SERVER_receive_done (client, GNUNET_OK);
2901     return;
2902   }
2903   GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
2904                                     &occ->other_peer_identity);
2905   /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2906   occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2907   GNUNET_asprintf (&occ->emsg,
2908                    "0x%llx: Timeout while connecting to CORE of peer with "
2909                    "id: %u", occ->op_id, occ->peer_id);
2910   occ->peer->reference_cnt++;
2911   occ->ch =
2912       GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2913                            &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2914                            GNUNET_NO, no_handlers);
2915   if (NULL == occ->ch)
2916     occ->timeout_task = 
2917         GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2918   else
2919     occ->timeout_task =
2920         GNUNET_SCHEDULER_add_delayed (TIMEOUT,
2921                                       &timeout_overlay_connect, occ);
2922   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2923 }
2924
2925
2926 /**
2927  * Function to cleanup RequestOverlayConnectContext and any associated tasks
2928  * with it
2929  *
2930  * @param rocc the RequestOverlayConnectContext
2931  */
2932 static void
2933 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
2934 {
2935   LOG_DEBUG ("0x%llx: Cleaning up rocc\n", rocc->op_id);
2936   if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
2937     GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
2938   if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
2939     GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
2940   if (NULL != rocc->ohh)
2941     GNUNET_TRANSPORT_offer_hello_cancel (rocc->ohh);
2942   if (NULL != rocc->tcc.tch)
2943     GNUNET_TRANSPORT_try_connect_cancel (rocc->tcc.tch);
2944   if (GNUNET_SCHEDULER_NO_TASK != rocc->tcc.task)
2945     GNUNET_SCHEDULER_cancel (rocc->tcc.task);
2946   GNUNET_TRANSPORT_disconnect (rocc->tcc.th);
2947   rocc->peer->reference_cnt--;
2948   if ((GNUNET_YES == rocc->peer->destroy_flag)
2949       && (0 == rocc->peer->reference_cnt))
2950     destroy_peer (rocc->peer);
2951   GNUNET_free_non_null (rocc->hello);
2952   GNUNET_CONTAINER_DLL_remove (roccq_head, roccq_tail, rocc);
2953   GNUNET_free (rocc);
2954 }
2955
2956
2957 /**
2958  * Task to timeout rocc and cleanit up
2959  *
2960  * @param cls the RequestOverlayConnectContext
2961  * @param tc the TaskContext from scheduler
2962  */
2963 static void
2964 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2965 {
2966   struct RequestOverlayConnectContext *rocc = cls;
2967   
2968   GNUNET_assert (rocc->timeout_rocc_task_id != GNUNET_SCHEDULER_NO_TASK);
2969   rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
2970   LOG_DEBUG ("0x%llx: rocc timed out\n", rocc->op_id);
2971   cleanup_rocc (rocc);
2972 }
2973
2974
2975 /**
2976  * Function called to notify transport users that another
2977  * peer connected to us.
2978  *
2979  * @param cls closure
2980  * @param new_peer the peer that connected
2981  * @param ats performance data
2982  * @param ats_count number of entries in ats (excluding 0-termination)
2983  */
2984 static void 
2985 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2986                           const struct GNUNET_ATS_Information * ats,
2987                           uint32_t ats_count)
2988 {
2989   struct RequestOverlayConnectContext *rocc = cls;
2990
2991   LOG_DEBUG ("0x%llx: Request Overlay connect notify\n", rocc->op_id);
2992   if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
2993     return;
2994   LOG_DEBUG ("0x%llx: Peer %4s connected\n", rocc->op_id,
2995              GNUNET_i2s (&rocc->a_id));
2996   cleanup_rocc (rocc);
2997 }
2998
2999
3000 /**
3001  * Task to offer the HELLO message to the peer and ask it to connect to the peer
3002  * whose identity is in RequestOverlayConnectContext
3003  *
3004  * @param cls the RequestOverlayConnectContext
3005  * @param tc the TaskContext from scheduler
3006  */
3007 static void
3008 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3009
3010
3011 /**
3012  * Task that is run when hello has been sent
3013  *
3014  * @param cls the overlay connect context
3015  * @param tc the scheduler task context; if tc->reason =
3016  *          GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
3017  *          GNUNET_SCHEDULER_REASON_READ_READY is succeeded
3018  */
3019 static void
3020 rocc_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3021 {
3022   struct RequestOverlayConnectContext *rocc = cls;
3023   
3024   rocc->ohh = NULL;
3025   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rocc->attempt_connect_task_id);
3026   LOG_DEBUG ("0x%llx: HELLO of peer %4s sent to local peer with id: %u\n",
3027              rocc->op_id, GNUNET_i2s (&rocc->a_id), rocc->peer->id);
3028   if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
3029   {
3030     GNUNET_break (0);
3031     rocc->attempt_connect_task_id =
3032         GNUNET_SCHEDULER_add_now (&attempt_connect_task,
3033                                   rocc);
3034     return;
3035   }
3036   if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
3037     return;
3038   rocc->tcc.task = GNUNET_SCHEDULER_add_now (&try_connect_task, &rocc->tcc);
3039 }
3040
3041
3042 /**
3043  * Task to offer the HELLO message to the peer and ask it to connect to the peer
3044  * whose identity is in RequestOverlayConnectContext
3045  *
3046  * @param cls the RequestOverlayConnectContext
3047  * @param tc the TaskContext from scheduler
3048  */
3049 static void
3050 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3051 {
3052   struct RequestOverlayConnectContext *rocc = cls;
3053
3054   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id);
3055   rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
3056   LOG_DEBUG ("0x%llx: Offering HELLO of peer %4s to local peer with id: %u\n",
3057              rocc->op_id, GNUNET_i2s (&rocc->a_id), rocc->peer->id);
3058   rocc->ohh = GNUNET_TRANSPORT_offer_hello (rocc->tcc.th, rocc->hello,
3059                                             rocc_hello_sent_cb, rocc);
3060   if (NULL == rocc->ohh)
3061     rocc->attempt_connect_task_id = 
3062         GNUNET_SCHEDULER_add_delayed 
3063         (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3064                                         100 + GNUNET_CRYPTO_random_u32
3065                                         (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
3066          &attempt_connect_task, rocc);
3067 }
3068
3069
3070 /**
3071  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
3072  *
3073  * @param cls NULL
3074  * @param client identification of the client
3075  * @param message the actual message
3076  */
3077 static void
3078 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
3079                                 const struct GNUNET_MessageHeader *message)
3080 {
3081   const struct GNUNET_TESTBED_RequestConnectMessage *msg;
3082   struct RequestOverlayConnectContext *rocc;
3083   struct Peer *peer;
3084   uint32_t peer_id;
3085   uint16_t msize;
3086   uint16_t hsize;
3087   
3088   msize = ntohs (message->size);
3089   if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
3090   {
3091     GNUNET_break (0);
3092     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3093     return;
3094   }  
3095   msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
3096   if ((NULL == msg->hello) || 
3097       (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
3098   {
3099     GNUNET_break (0);
3100     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3101     return;
3102   }
3103   hsize = ntohs (msg->hello->size);
3104   if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
3105   {
3106     GNUNET_break (0);
3107     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3108     return;
3109   }
3110   peer_id = ntohl (msg->peer);
3111   if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
3112   {
3113     GNUNET_break_op (0);
3114     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3115     return;
3116   }
3117   if (GNUNET_YES == peer->is_remote)
3118   {
3119     struct GNUNET_MessageHeader *msg2;
3120     
3121     msg2 = GNUNET_copy_message (message);
3122     GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller, msg2);
3123     GNUNET_SERVER_receive_done (client, GNUNET_OK);
3124     return;
3125   }
3126   rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
3127   rocc->op_id = GNUNET_ntohll (msg->operation_id);
3128   GNUNET_CONTAINER_DLL_insert_tail (roccq_head, roccq_tail, rocc);
3129   memcpy (&rocc->a_id, &msg->peer_identity,
3130           sizeof (struct GNUNET_PeerIdentity));
3131   LOG_DEBUG ("Received request for overlay connection with op_id: 0x%llx "
3132              "from local peer %u to peer %4s with hello size: %u\n",
3133              rocc->op_id,
3134              peer_id,
3135              GNUNET_i2s (&rocc->a_id),
3136              hsize);
3137   rocc->peer = peer;
3138   rocc->peer->reference_cnt++;
3139   rocc->tcc.op_id = rocc->op_id;
3140   rocc->tcc.th = GNUNET_TRANSPORT_connect (rocc->peer->details.local.cfg, NULL, rocc,
3141                                            NULL, &transport_connect_notify, NULL);
3142   if (NULL == rocc->tcc.th)
3143   {
3144     GNUNET_break (0);
3145     GNUNET_free (rocc);
3146     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3147     return;
3148   }
3149   rocc->tcc.pid = &rocc->a_id;
3150   rocc->hello = GNUNET_malloc (hsize);
3151   memcpy (rocc->hello, msg->hello, hsize);
3152   rocc->attempt_connect_task_id =
3153       GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
3154   rocc->timeout_rocc_task_id =
3155       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
3156   GNUNET_SERVER_receive_done (client, GNUNET_OK);
3157 }
3158
3159
3160 /**
3161  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
3162  *
3163  * @param cls NULL
3164  * @param client identification of the client
3165  * @param message the actual message
3166  */
3167 static void
3168 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
3169                          const struct GNUNET_MessageHeader *message)
3170 {
3171   struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
3172   struct Slave *slave;  
3173   struct GNUNET_TESTBED_SlaveConfiguration *reply;
3174   char *config;
3175   char *xconfig;
3176   size_t config_size;
3177   size_t xconfig_size;
3178   size_t reply_size;
3179   uint64_t op_id;
3180   uint32_t slave_id;
3181
3182   msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
3183   slave_id = ntohl (msg->slave_id);
3184   op_id = GNUNET_ntohll (msg->operation_id);
3185   if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
3186   {
3187     /* FIXME: Add forwardings for this type of message here.. */
3188     send_operation_fail_msg (client, op_id, "Slave not found");
3189     GNUNET_SERVER_receive_done (client, GNUNET_OK);
3190     return;
3191   }
3192   slave = slave_list[slave_id];
3193   if (NULL == slave->cfg)
3194   {
3195     send_operation_fail_msg (client, op_id,
3196                              "Configuration not found (slave not started by me)");
3197     GNUNET_SERVER_receive_done (client, GNUNET_OK);
3198     return;
3199   }
3200   config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
3201   xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size, 
3202                                                   &xconfig);
3203   GNUNET_free (config);  
3204   reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
3205   GNUNET_break (reply_size <= UINT16_MAX);
3206   GNUNET_break (config_size <= UINT16_MAX);
3207   reply = GNUNET_realloc (xconfig, reply_size);
3208   (void) memmove (&reply[1], reply, xconfig_size);
3209   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
3210   reply->header.size = htons ((uint16_t) reply_size);
3211   reply->slave_id = msg->slave_id;
3212   reply->operation_id = msg->operation_id;
3213   reply->config_size = htons ((uint16_t) config_size);
3214   queue_message (client, &reply->header);
3215   GNUNET_SERVER_receive_done (client, GNUNET_OK);
3216 }
3217
3218
3219 /**
3220  * Iterator over hash map entries.
3221  *
3222  * @param cls closure
3223  * @param key current key code
3224  * @param value value in the hash map
3225  * @return GNUNET_YES if we should continue to
3226  *         iterate,
3227  *         GNUNET_NO if not.
3228  */
3229 static int
3230 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
3231 {
3232   struct SharedService *ss = value;
3233
3234   GNUNET_assert (GNUNET_YES ==
3235                  GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
3236   GNUNET_free (ss->name);
3237   GNUNET_free (ss);
3238   return GNUNET_YES;
3239 }
3240
3241
3242 /**
3243  * Iterator for freeing hash map entries in a slave's reghost_map
3244  *
3245  * @param cls handle to the slave
3246  * @param key current key code
3247  * @param value value in the hash map
3248  * @return GNUNET_YES if we should continue to
3249  *         iterate,
3250  *         GNUNET_NO if not.
3251  */
3252 static int 
3253 reghost_free_iterator (void *cls,
3254                        const struct GNUNET_HashCode * key,
3255                        void *value)
3256 {
3257   struct Slave *slave = cls;
3258   struct RegisteredHostContext *rhc = value;
3259   struct ForwardedOverlayConnectContext *focc;
3260
3261   GNUNET_assert (GNUNET_YES ==
3262                  GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map,
3263                                                        key, value));
3264   while (NULL != (focc = rhc->focc_dll_head))
3265   {
3266     GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head,
3267                                  rhc->focc_dll_tail,
3268                                  focc);
3269     cleanup_focc (focc);
3270   }
3271   if (NULL != rhc->sub_op)
3272     GNUNET_TESTBED_operation_done (rhc->sub_op);
3273   if (NULL != rhc->client)
3274   GNUNET_SERVER_client_drop (rhc->client);
3275   GNUNET_free (value);
3276   return GNUNET_YES;
3277 }
3278
3279
3280 /**
3281  * Task to clean up and shutdown nicely
3282  *
3283  * @param cls NULL
3284  * @param tc the TaskContext from scheduler
3285  */
3286 static void
3287 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3288 {
3289   struct LCFContextQueue *lcfq;
3290   struct OverlayConnectContext *occ;
3291   struct RequestOverlayConnectContext *rocc;
3292   struct ForwardedOperationContext *fopc;
3293   struct MessageQueue *mq_entry;
3294   uint32_t id;
3295
3296   shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
3297   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
3298   (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
3299                                                 NULL);
3300   GNUNET_CONTAINER_multihashmap_destroy (ss_map);
3301   /* cleanup any remaining forwarded operations */
3302   while (NULL != (fopc = fopcq_head))
3303   {
3304     GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
3305     GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
3306     if (GNUNET_SCHEDULER_NO_TASK != fopc->timeout_task)
3307       GNUNET_SCHEDULER_cancel (fopc->timeout_task);
3308     GNUNET_SERVER_client_drop (fopc->client);
3309     switch (fopc->type)
3310     {
3311     case OP_PEER_CREATE:
3312       GNUNET_free (fopc->cls);
3313       break;
3314     case OP_PEER_START:
3315     case OP_PEER_STOP:
3316     case OP_PEER_DESTROY:
3317     case OP_PEER_INFO:
3318     case OP_OVERLAY_CONNECT:
3319     case OP_LINK_CONTROLLERS:
3320     case OP_GET_SLAVE_CONFIG:
3321       break;
3322     case OP_FORWARDED:
3323       GNUNET_assert (0);
3324     };
3325     GNUNET_free (fopc);
3326   }
3327   if (NULL != lcfq_head)
3328   {
3329     if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
3330     {
3331       GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
3332       lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
3333     }
3334   }
3335   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
3336   for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
3337   {
3338     GNUNET_free (lcfq->lcf->msg);
3339     GNUNET_free (lcfq->lcf);
3340     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
3341     GNUNET_free (lcfq);
3342   }
3343   while (NULL != (occ = occq_head))
3344     cleanup_occ (occ);
3345   while (NULL != (rocc = roccq_head))
3346     cleanup_rocc (rocc);
3347   /* Clear peer list */
3348   for (id = 0; id < peer_list_size; id++)
3349     if (NULL != peer_list[id])
3350     {
3351       /* If destroy flag is set it means that this peer should have been
3352          destroyed by a context which we destroy before */
3353       GNUNET_break (GNUNET_NO == peer_list[id]->destroy_flag);
3354       /* counter should be zero as we free all contexts before */
3355       GNUNET_break (0 == peer_list[id]->reference_cnt);      
3356       if ( (GNUNET_NO == peer_list[id]->is_remote)
3357            && (GNUNET_YES == peer_list[id]->details.local.is_running))
3358         GNUNET_TESTING_peer_kill (peer_list[id]->details.local.peer);
3359     }
3360   for (id = 0; id < peer_list_size; id++)
3361     if (NULL != peer_list[id])
3362     {
3363       if (GNUNET_NO == peer_list[id]->is_remote)
3364       {
3365         if (GNUNET_YES == peer_list[id]->details.local.is_running)
3366           GNUNET_TESTING_peer_wait (peer_list[id]->details.local.peer);
3367         GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
3368         GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
3369       }
3370       GNUNET_free (peer_list[id]);
3371     }
3372   GNUNET_free_non_null (peer_list);
3373   /* Clear host list */
3374   for (id = 0; id < host_list_size; id++)
3375     if (NULL != host_list[id])
3376       GNUNET_TESTBED_host_destroy (host_list[id]);
3377   GNUNET_free_non_null (host_list);
3378   /* Clear route list */
3379   for (id = 0; id < route_list_size; id++)
3380     if (NULL != route_list[id])
3381       GNUNET_free (route_list[id]);
3382   GNUNET_free_non_null (route_list);
3383   /* Clear slave_list */
3384   for (id = 0; id < slave_list_size; id++)
3385     if (NULL != slave_list[id])
3386     {
3387       struct HostRegistration *hr_entry;
3388       
3389       while (NULL != (hr_entry = slave_list[id]->hr_dll_head))
3390       {
3391         GNUNET_CONTAINER_DLL_remove (slave_list[id]->hr_dll_head,
3392                                      slave_list[id]->hr_dll_tail,
3393                                      hr_entry);
3394         GNUNET_free (hr_entry);
3395       }
3396       if (NULL != slave_list[id]->rhandle)
3397         GNUNET_TESTBED_cancel_registration (slave_list[id]->rhandle);
3398       (void) GNUNET_CONTAINER_multihashmap_iterate (slave_list[id]->reghost_map,
3399                                                     reghost_free_iterator,
3400                                                     slave_list[id]);
3401       GNUNET_CONTAINER_multihashmap_destroy (slave_list[id]->reghost_map);
3402       if (NULL != slave_list[id]->cfg)
3403         GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
3404       if (NULL != slave_list[id]->controller)
3405         GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
3406       if (NULL != slave_list[id]->controller_proc)
3407         GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
3408       GNUNET_free (slave_list[id]);
3409     }
3410   GNUNET_free_non_null (slave_list);
3411   if (NULL != master_context)
3412   {
3413     GNUNET_free_non_null (master_context->master_ip);
3414     if (NULL != master_context->system)
3415       GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
3416     GNUNET_SERVER_client_drop (master_context->client);
3417     GNUNET_free (master_context);
3418     master_context = NULL;
3419   }
3420   if (NULL != transmit_handle)
3421     GNUNET_SERVER_notify_transmit_ready_cancel (transmit_handle);  
3422   while (NULL != (mq_entry = mq_head))
3423   {
3424     GNUNET_free (mq_entry->msg);
3425     GNUNET_SERVER_client_drop (mq_entry->client);
3426     GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);    
3427     GNUNET_free (mq_entry);
3428   }  
3429   GNUNET_free_non_null (hostname);
3430   GNUNET_CONFIGURATION_destroy (our_config);
3431   /* Free hello cache */
3432   if (NULL != hello_cache)
3433     GNUNET_assert
3434         (GNUNET_CONTAINER_multihashmap_size (hello_cache) <= hello_cache_size);
3435   while (NULL != lru_hcache_head)
3436     hello_cache_remove (lru_hcache_head);
3437   if (NULL != hello_cache)
3438   {
3439     GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (hello_cache));
3440     GNUNET_CONTAINER_multihashmap_destroy (hello_cache);
3441   }
3442 }
3443
3444
3445 /**
3446  * Callback for client disconnect
3447  *
3448  * @param cls NULL
3449  * @param client the client which has disconnected
3450  */
3451 static void
3452 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
3453 {
3454   if (NULL == master_context)
3455     return;
3456   if (client == master_context->client)
3457   {
3458     LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
3459     /* should not be needed as we're terminated by failure to read
3460      * from stdin, but if stdin fails for some reason, this shouldn't
3461      * hurt for now --- might need to revise this later if we ever
3462      * decide that master connections might be temporarily down
3463      * for some reason */
3464     //GNUNET_SCHEDULER_shutdown ();
3465   }
3466 }
3467
3468
3469 /**
3470  * Testbed setup
3471  *
3472  * @param cls closure
3473  * @param server the initialized server
3474  * @param cfg configuration to use
3475  */
3476 static void
3477 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
3478              const struct GNUNET_CONFIGURATION_Handle *cfg)
3479 {
3480   static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
3481     {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
3482     {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
3483     {&handle_configure_shared_service, NULL,
3484      GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
3485     {&handle_link_controllers, NULL,
3486      GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
3487     {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
3488     {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
3489      sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
3490     {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
3491      sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
3492     {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
3493      sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
3494     {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
3495      sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
3496     {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
3497      sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
3498     {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
3499      0},
3500     {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
3501      sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
3502     {NULL}
3503   };
3504   char *logfile;
3505   unsigned long long num;
3506
3507   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
3508                                                             "TESTBED",
3509                                                             "LOG_FILE",
3510                                                             &logfile))
3511   {
3512     GNUNET_break (GNUNET_OK == GNUNET_log_setup ("testbed", "DEBUG", logfile));
3513     GNUNET_free (logfile);
3514   }
3515   GNUNET_assert (GNUNET_OK ==  
3516                  GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
3517                                                         "HELLO_CACHE_SIZE",
3518                                                         &num));
3519   hello_cache_size = (unsigned int) num;
3520   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string 
3521                  (cfg, "testbed", "HOSTNAME", &hostname));
3522   our_config = GNUNET_CONFIGURATION_dup (cfg);
3523   GNUNET_SERVER_add_handlers (server, message_handlers);
3524   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
3525   ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
3526   if (1 < hello_cache_size)
3527     hello_cache = GNUNET_CONTAINER_multihashmap_create (hello_cache_size / 2,
3528                                                         GNUNET_YES);
3529   shutdown_task_id =
3530       GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
3531                                                   GNUNET_SCHEDULER_PRIORITY_IDLE,
3532                                                   &shutdown_task, NULL);
3533   LOG_DEBUG ("Testbed startup complete\n");
3534   event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
3535 }
3536
3537
3538 /**
3539  * The starting point of execution
3540  */
3541 int
3542 main (int argc, char *const *argv)
3543 {
3544   //sleep (15);                 /* Debugging */
3545   return (GNUNET_OK ==
3546           GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
3547                               &testbed_run, NULL)) ? 0 : 1;
3548 }
3549
3550 /* end of gnunet-service-testbed.c */