- stop using message types of payload on service side
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh.c
index e8ce53af212ce4039aa14bf2d32fb26a93f591ab..5b7e9991f957b468da48936bbfb8c820db6d7880 100644 (file)
 #include "mesh_protocol.h"
 #include "mesh_tunnel_tree.h"
 #include "block_mesh.h"
-#include "mesh_block_lib.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_statistics_service.h"
 #include "gnunet_regex_lib.h"
 
 #define MESH_BLOOM_SIZE         128
 
+#define MESH_DEBUG_REGEX        GNUNET_YES
 #define MESH_DEBUG_DHT          GNUNET_NO
 #define MESH_DEBUG_CONNECTION   GNUNET_NO
 #define MESH_DEBUG_TIMING       __LINUX__ && GNUNET_NO
 
+#define MESH_MAX_POLL_TIME      GNUNET_TIME_relative_multiply (\
+                                  GNUNET_TIME_UNIT_MINUTES,\
+                                  10)
+
 #if MESH_DEBUG_CONNECTION
 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 #else
 #define DEBUG_DHT(...)
 #endif
 
+#if MESH_DEBUG_REGEX
+#define DEBUG_REGEX(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
+#else
+#define DEBUG_REGEX(...)
+#endif
+
 #if MESH_DEBUG_TIMING
 #include <time.h>
 double __sum;
@@ -170,6 +180,79 @@ struct MeshPeerQueue
 };
 
 
+/**
+ * Struct to store regex information announced by clients.
+ */
+struct MeshRegexDescriptor
+{
+    /**
+     * Regular expression itself.
+     */
+  char *regex;
+
+    /**
+     * How many characters per edge can we squeeze?
+     */
+  uint16_t compression;
+
+    /**
+     * Handle to announce the regex.
+     */
+  struct GNUNET_REGEX_announce_handle *h;
+};
+
+
+/**
+ * Struct to keep information of searches of services described by a regex
+ * using a user-provided string service description.
+ */
+struct MeshRegexSearchInfo
+{
+    /**
+     * Which tunnel is this for
+     */
+  struct MeshTunnel *t;
+
+    /**
+     * User provided description of the searched service.
+     */
+  char *description;
+
+    /**
+     * Regex search handle.
+     */
+  struct GNUNET_REGEX_search_handle *search_handle;
+
+    /**
+     * Peer that is connecting via connect_by_string. When connected, free ctx.
+     */
+  GNUNET_PEER_Id peer;
+
+    /**
+     * Other peers that are found but not yet being connected to.
+     */
+  GNUNET_PEER_Id *peers;
+
+    /**
+     * Number of elements in peers.
+     */
+  unsigned int n_peers;
+
+    /**
+     * Next peer to try to connect to.
+     */
+  unsigned int i_peer;
+
+    /**
+     * Timeout for a connect attempt.
+     * When reached, try to connect to a different peer, if any. If not,
+     * try the same peer again.
+     */
+  GNUNET_SCHEDULER_TaskIdentifier timeout;
+
+};
+
+
 /**
  * Struct containing all info possibly needed to build a package when called
  * back by core.
@@ -205,6 +288,11 @@ struct MeshPeerInfo
      */
   struct GNUNET_TIME_Absolute last_contact;
 
+    /**
+     * Task handler for delayed connect task;
+     */
+  GNUNET_SCHEDULER_TaskIdentifier connect_task;
+
     /**
      * Number of attempts to reconnect so far
      */
@@ -330,11 +418,16 @@ struct MeshTunnel
      */
   uint32_t bck_pid;
 
-  /**
+    /**
      * SKIP value for this tunnel.
      */
   uint32_t skip;
 
+    /**
+     * Force sending ACK? Flag to allow duplicate ACK on POLL.
+     */
+  int force_ack;
+
     /**
      * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
      * Contains the Flow Control info: FWD ACK value received,
@@ -347,10 +440,10 @@ struct MeshTunnel
      */
   uint32_t last_fwd_ack;
 
-  /**
-   * BCK ACK value received from the hop towards the owner of the tunnel,
-   * (previous node / owner): up to what message PID can we sent back to him.
-   */
+    /**
+     * BCK ACK value received from the hop towards the owner of the tunnel,
+     * (previous node / owner): up to what message PID can we sent back to him.
+     */
   uint32_t bck_ack;
 
     /**
@@ -373,6 +466,11 @@ struct MeshTunnel
     */
    unsigned int bck_queue_max;
 
+    /**
+     * Task to poll peer in case of a stall.
+     */
+   GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
+
     /**
      * Last time the tunnel was used
      */
@@ -389,11 +487,6 @@ struct MeshTunnel
      */
   unsigned int peers_ready;
 
-    /**
-     * Number of peers that have been added to the tunnel
-     */
-  unsigned int peers_total;
-
     /**
      * Client owner of the tunnel, if any
      */
@@ -455,9 +548,9 @@ struct MeshTunnel
   struct GNUNET_DHT_GetHandle *dht_get_type;
 
     /**
-     * Initial context of the regex search for a connect_by_string
+     * Handle for the regex search for a connect_by_string
      */
-  struct MeshRegexSearchContext *regex_ctx;
+  struct MeshRegexSearchInfo *regex_search;
 
     /**
      * Task to keep the used paths alive
@@ -478,6 +571,16 @@ struct MeshTunnel
      * when the queue is empty.
      */
   int destroy;
+
+    /**
+     * Total messages pending for this tunnels, payload or not.
+     */
+  unsigned int pending_messages;
+
+  /**
+   * If the tunnel is empty, destoy it.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
 };
 
 
@@ -499,7 +602,12 @@ struct MeshTunnelChildInfo
     /**
      * Last sent PID.
      */
-  uint32_t pid;
+  uint32_t fwd_pid;
+
+    /**
+     * Last received PID.
+     */
+  uint32_t bck_pid;
 
     /**
      * Maximum PID allowed (FWD ACK received).
@@ -527,6 +635,21 @@ struct MeshTunnelChildInfo
      * How many elements are already in the buffer.
      */
   unsigned int send_buffer_n;
+
+    /**
+     * Tunnel this info is about
+     */
+  struct MeshTunnel *t;
+
+    /**
+     * Task to poll peer in case of a stall.
+     */
+  GNUNET_SCHEDULER_TaskIdentifier fc_poll;
+
+     /**
+      * Time to use for next polling call.
+      */
+   struct GNUNET_TIME_Relative fc_poll_time;
 };
 
 
@@ -643,7 +766,7 @@ struct MeshClient
   struct GNUNET_SERVER_Client *handle;
 
     /**
-     * Applications that this client has claimed to provide
+     * Applications that this client has claimed to provide: H(app) = app.
      */
   struct GNUNET_CONTAINER_MultiHashMap *apps;
 
@@ -666,7 +789,7 @@ struct MeshClient
     /**
      * Regular expressions describing the services offered by this client.
      */
-  char **regexes; // FIXME add timeout? API to remove a regex?
+  struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
 
     /**
      * Number of regular expressions in regexes.
@@ -678,107 +801,13 @@ struct MeshClient
      */
   GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
 
-};
-
-
-/**
- * Struct to keep information of searches of services described by a regex
- * using a user-provided string service description.
- */
-struct MeshRegexSearchInfo
-{
-    /**
-     * Which tunnel is this for
-     */
-  struct MeshTunnel *t;
-
-    /**
-     * User provided description of the searched service.
-     */
-  char *description;
-
-    /**
-     * Part of the description already consumed by the search.
-     */
-  size_t position;
-
-    /**
-     * Running DHT GETs.
-     */
-  struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
-
-    /**
-     * Results from running DHT GETs.
-     */
-  struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
-
-    /**
-     * Contexts, for each running DHT GET. Free all on end of search.
-     */
-  struct MeshRegexSearchContext **contexts;
-
     /**
-     * Number of contexts (branches/steps in search).
+     * Tmp store for partially retrieved regex.
      */
-  unsigned int n_contexts;
-
-    /**
-     * Peer that is connecting via connect_by_string. When connected, free ctx.
-     */
-  GNUNET_PEER_Id peer;
-
-    /**
-     * Other peers that are found but not yet being connected to.
-     */
-  GNUNET_PEER_Id *peers;
-
-    /**
-     * Number of elements in peers.
-     */
-  unsigned int n_peers;
-
-    /**
-     * Next peer to try to connect to.
-     */
-  unsigned int i_peer;
-
-    /**
-     * Timeout for a connect attempt.
-     * When reached, try to connect to a different peer, if any. If not,
-     * try the same peer again.
-     */
-  GNUNET_SCHEDULER_TaskIdentifier timeout;
+  char *partial_regex;
 
 };
 
-/**
- * Struct to keep state of running searches that have consumed a part of
- * the inital string.
- */
-struct MeshRegexSearchContext
-{
-    /**
-     * Part of the description already consumed by
-     * this particular search branch.
-     */
-  size_t position;
-
-    /**
-     * Information about the search.
-     */
-  struct MeshRegexSearchInfo *info;
-
-    /**
-     * We just want to look for one edge, the longer the better.
-     * Keep its length.
-     */
-  unsigned int longest_match;
-
-    /**
-     * Destination hash of the longest match.
-     */
-  struct GNUNET_HashCode hash;
-};
 
 /******************************************************************************/
 /************************      DEBUG FUNCTIONS     ****************************/
@@ -814,24 +843,62 @@ unsigned int debug_bck_ack;
 /***********************      GLOBAL VARIABLES     ****************************/
 /******************************************************************************/
 
+/************************** Configuration parameters **************************/
+
 /**
- * Configuration parameters
+ * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
  */
 static struct GNUNET_TIME_Relative refresh_path_time;
+
+/**
+ * How often to PUT local application numbers in the DHT.
+ */
 static struct GNUNET_TIME_Relative app_announce_time;
+
+/**
+ * How often to PUT own ID in the DHT.
+ */
 static struct GNUNET_TIME_Relative id_announce_time;
-static struct GNUNET_TIME_Relative unacknowledged_wait_time;
+
+/**
+ * Maximum time allowed to connect to a peer found by string.
+ */
 static struct GNUNET_TIME_Relative connect_timeout;
+
+/**
+ * Default TTL for payload packets.
+ */
 static unsigned long long default_ttl;
+
+/**
+ * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
+ */
 static unsigned long long dht_replication_level;
+
+/**
+ * How many tunnels are we willing to maintain.
+ * Local tunnels are always allowed, even if there are more tunnels than max.
+ */
 static unsigned long long max_tunnels;
+
+/**
+ * How many messages *in total* are we willing to queue, divided by number of 
+ * tunnels to get tunnel queue size.
+ */
 static unsigned long long max_msgs_queue;
 
+/**
+ * How many peers do we want to remember?
+ */
+static unsigned long long max_peers;
+
+
+/*************************** Static global variables **************************/
 
 /**
  * Hostkey generation context
  */
-static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
+static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
 
 /**
  * DLL with all the clients, head.
@@ -907,12 +974,12 @@ static struct GNUNET_PeerIdentity my_full_id;
 /**
  * Own private key.
  */
-static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
+static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
 
 /**
  * Own public key.
  */
-static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
+static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
 
 /**
  * Tunnel ID for the next created tunnel (global tunnel number).
@@ -925,7 +992,7 @@ static MESH_TunnelNumber next_tid;
 static MESH_TunnelNumber next_local_tid;
 
 /**
- * All application types provided by this peer.
+ * All application types provided by this peer: H(app) = *Client.
  */
 static struct GNUNET_CONTAINER_MultiHashMap *applications;
 
@@ -954,8 +1021,6 @@ unsigned int next_client_id;
 /***********************         DECLARATIONS        **************************/
 /******************************************************************************/
 
-/* FIXME move declarations here */
-
 /**
  * Function to process paths received for a new peer addition. The recorded
  * paths form the initial tunnel, which can be optimized later.
@@ -979,56 +1044,15 @@ dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
 
 
 /**
- * Function to process DHT string to regex matching.
- * Called on each result obtained for the DHT search.
- *
- * @param cls closure (search context)
- * @param exp when will this value expire
- * @param key key of the result
- * @param get_path path of the get request (not used)
- * @param get_path_length lenght of get_path (not used)
- * @param put_path path of the put request (not used)
- * @param put_path_length length of the put_path (not used)
- * @param type type of the result
- * @param size number of bytes in data
- * @param data pointer to the result data
+ * Retrieve the MeshPeerInfo stucture associated with the peer, create one
+ * and insert it in the appropiate structures if the peer is not known yet.
  *
- * TODO: re-issue the request after certain time? cancel after X results?
- */
-static void
-dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
-                        const struct GNUNET_HashCode * key,
-                        const struct GNUNET_PeerIdentity *get_path,
-                        unsigned int get_path_length,
-                        const struct GNUNET_PeerIdentity *put_path,
-                        unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
-                        size_t size, const void *data);
-
-
-/**
- * Function to process DHT string to regex matching.
- * Called on each result obtained for the DHT search.
+ * @param peer Full identity of the peer.
  *
- * @param cls closure (search context)
- * @param exp when will this value expire
- * @param key key of the result
- * @param get_path path of the get request (not used)
- * @param get_path_length lenght of get_path (not used)
- * @param put_path path of the put request (not used)
- * @param put_path_length length of the put_path (not used)
- * @param type type of the result
- * @param size number of bytes in data
- * @param data pointer to the result data
+ * @return Existing or newly created peer info.
  */
-static void
-dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
-                               const struct GNUNET_HashCode * key,
-                               const struct GNUNET_PeerIdentity *get_path,
-                               unsigned int get_path_length,
-                               const struct GNUNET_PeerIdentity *put_path,
-                               unsigned int put_path_length,
-                               enum GNUNET_BLOCK_Type type,
-                               size_t size, const void *data);
+static struct MeshPeerInfo *
+peer_info_get (const struct GNUNET_PeerIdentity *peer);
 
 
 /**
@@ -1056,6 +1080,29 @@ static void
 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
 
 
+/**
+ * Build a PeerPath from the paths returned from the DHT, reversing the paths
+ * to obtain a local peer -> destination path and interning the peer ids.
+ *
+ * @return Newly allocated and created path
+ */
+static struct MeshPeerPath *
+path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
+                     unsigned int get_path_length,
+                     const struct GNUNET_PeerIdentity *put_path,
+                     unsigned int put_path_length);
+
+
+/**
+ * Adds a path to the peer_infos of all the peers in the path
+ *
+ * @param p Path to process.
+ * @param confirmed Whether we know if the path works or not.
+ */
+static void
+path_add_to_peers (struct MeshPeerPath *p, int confirmed);
+
+
 /**
  * Add a peer to a tunnel, accomodating paths accordingly and initializing all
  * needed rescources.
@@ -1090,7 +1137,7 @@ tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
  * @return tunnel handler, NULL if doesn't exist.
  */
 static struct MeshTunnel *
-tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
+tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
 
 
 /**
@@ -1140,33 +1187,6 @@ static void
 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
 
 
-/**
- * Jump to the next edge, with the longest matching token.
- *
- * @param block Block found in the DHT.
- * @param size Size of the block.
- * @param ctx Context of the search.
- *
- * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
- */
-static void
-regex_next_edge (const struct MeshRegexBlock *block,
-                 size_t size,
-                 struct MeshRegexSearchContext *ctx);
-
-
-/**
- * Find a path to a peer that offers a regex servcie compatible
- * with a given string.
- * 
- * @param key The key of the accepting state.
- * @param ctx Context containing info about the string, tunnel, etc.
- */
-static void
-regex_find_path (const struct GNUNET_HashCode *key,
-                 struct MeshRegexSearchContext *ctx);
-
-
 /**
  * @brief Queue and pass message to core when possible.
  * 
@@ -1225,378 +1245,27 @@ queue_get_next (const struct MeshPeerInfo *peer);
  * @return number of bytes written to buf
  */
 static size_t
-queue_send (void *cls, size_t size, void *buf);
-
-/******************************************************************************/
-/************************         ITERATORS        ****************************/
-/******************************************************************************/
-
-/**
- * Iterator over found existing mesh regex blocks that match an ongoing search.
- *
- * @param cls closure
- * @param key current key code
- * @param value value in the hash map
- * @return GNUNET_YES if we should continue to iterate,
- *         GNUNET_NO if not.
- */
-static int
-regex_result_iterator (void *cls,
-                       const struct GNUNET_HashCode * key,
-                       void *value)
-{
-  struct MeshRegexBlock *block = value;
-  struct MeshRegexSearchContext *ctx = cls;
-
-  if (GNUNET_YES == ntohl(block->accepting) &&
-      ctx->position == strlen (ctx->info->description))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
-    regex_find_path (key, ctx);
-    return GNUNET_YES; // We found an accept state!
-  }
-  else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
-                ctx->position, strlen(ctx->info->description),
-                ntohl(block->accepting));
-
-  }
-  regex_next_edge(block, SIZE_MAX, ctx);
-
-  return GNUNET_YES;
-}
-
-
-/**
- * Iterator over edges in a regex block retrieved from the DHT.
- *
- * @param cls Closure (context of the search).
- * @param token Token that follows to next state.
- * @param len Lenght of token.
- * @param key Hash of next state.
- *
- * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
- */
-static int
-regex_edge_iterator (void *cls,
-                     const char *token,
-                     size_t len,
-                     const struct GNUNET_HashCode *key)
-{
-  struct MeshRegexSearchContext *ctx = cls;
-  struct MeshRegexSearchInfo *info = ctx->info;
-  char *current;
-  size_t current_len;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*    Start of regex edge iterator\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     descr : %s\n", info->description);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     posit : %u\n", ctx->position);
-  current = &info->description[ctx->position];
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     currt : %s\n", current);
-  current_len = strlen (info->description) - ctx->position;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     ctlen : %u\n", current_len);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     tklen : %u\n", len);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     tk[0] : %c\n", token[0]);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     nextk : %s\n", GNUNET_h2s(key));
-  if (len > current_len)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     Token too long, END\n");
-    return GNUNET_YES; // Token too long, wont match
-  }
-  if (0 != strncmp (current, token, len))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     Token doesn't match, END\n");
-    return GNUNET_YES; // Token doesn't match
-  }
-
-  if (len > ctx->longest_match)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     Token is longer, KEEP\n");
-    ctx->longest_match = len;
-    ctx->hash = *key;
-  }
-  else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     Token is not longer, IGNORE\n");
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*    End of regex edge iterator\n");
-  return GNUNET_YES;
-}
-
-
-/**
- * Jump to the next edge, with the longest matching token.
- *
- * @param block Block found in the DHT.
- * @param size Size of the block.
- * @param ctx Context of the search.
- *
- * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
- */
-static void
-regex_next_edge (const struct MeshRegexBlock *block,
-                 size_t size,
-                 struct MeshRegexSearchContext *ctx)
-{
-  struct MeshRegexSearchContext *new_ctx;
-  struct MeshRegexSearchInfo *info = ctx->info;
-  struct GNUNET_DHT_GetHandle *get_h;
-
-  int result;
-
-  /* Find the longest match for the current string position, 
-   * among tokens in the given block */
-  ctx->longest_match = 0;
-  result = GNUNET_MESH_regex_block_iterate (block, size,
-                                            &regex_edge_iterator, ctx);
-  GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
-
-  /* Did anything match? */
-  if (0 == ctx->longest_match)
-    return;
-
-  new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
-  new_ctx->info = info;
-  new_ctx->position = ctx->position + ctx->longest_match;
-  GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
-
-  /* Check whether we already have a DHT GET running for it */
-  if (GNUNET_YES ==
-      GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*     GET running, END\n");
-    GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
-                                                &ctx->hash,
-                                                &regex_result_iterator,
-                                                new_ctx);
-    return; // We are already looking for it
-  }
-
-  /* Start search in DHT */
-  get_h = 
-      GNUNET_DHT_get_start (dht_handle,    /* handle */
-                            GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
-                            &ctx->hash,     /* key to search */
-                            dht_replication_level, /* replication level */
-                            GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
-                            NULL,       /* xquery */ // FIXME BLOOMFILTER
-                            0,     /* xquery bits */ // FIXME BLOOMFILTER SIZE
-                            &dht_get_string_handler, new_ctx);
-  if (GNUNET_OK !=
-      GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
-                                        &ctx->hash,
-                                        get_h,
-                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
-  {
-    GNUNET_break (0);
-    return;
-  }
-}
-
-
-/**
- * Iterator over hash map entries to cancel DHT GET requests after a
- * successful connect_by_string.
- *
- * @param cls Closure (unused).
- * @param key Current key code (unused).
- * @param value Value in the hash map (get handle).
- * @return GNUNET_YES if we should continue to iterate,
- *         GNUNET_NO if not.
- */
-static int
-regex_cancel_dht_get (void *cls,
-                      const struct GNUNET_HashCode * key,
-                      void *value)
-{
-  struct GNUNET_DHT_GetHandle *h = value;
-
-  GNUNET_DHT_get_stop (h);
-  return GNUNET_YES;
-}
-
-
-/**
- * Iterator over hash map entries to free MeshRegexBlocks stored during the
- * search for connect_by_string.
- *
- * @param cls Closure (unused).
- * @param key Current key code (unused).
- * @param value MeshRegexBlock in the hash map.
- * @return GNUNET_YES if we should continue to iterate,
- *         GNUNET_NO if not.
- */
-static int
-regex_free_result (void *cls,
-                   const struct GNUNET_HashCode * key,
-                   void *value)
-{
-
-  GNUNET_free (value);
-  return GNUNET_YES;
-}
-
-
-/**
- * Regex callback iterator to store own service description in the DHT.
- *
- * @param cls closure.
- * @param key hash for current state.
- * @param proof proof for current state.
- * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
- * @param num_edges number of edges leaving current state.
- * @param edges edges leaving current state.
- */
-void
-regex_iterator (void *cls,
-                const struct GNUNET_HashCode *key,
-                const char *proof,
-                int accepting,
-                unsigned int num_edges,
-                const struct GNUNET_REGEX_Edge *edges)
-{
-    struct MeshRegexBlock *block;
-    struct MeshRegexEdge *block_edge;
-    enum GNUNET_DHT_RouteOption opt;
-    size_t size;
-    size_t len;
-    unsigned int i;
-    unsigned int offset;
-    char *aux;
-
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  regex dht put for state %s\n",
-                GNUNET_h2s(key));
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "   proof: %s\n",
-                proof);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "   num edges: %u\n",
-                num_edges);
-
-    opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
-    if (GNUNET_YES == accepting)
-    {
-        struct MeshRegexAccept block;
-
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "   state %s is accepting, putting own id\n",
-                    GNUNET_h2s(key));
-        size = sizeof (block);
-        block.key = *key;
-        block.id = my_full_id;
-        (void)
-        GNUNET_DHT_put(dht_handle, key,
-                       dht_replication_level,
-                       opt | GNUNET_DHT_RO_RECORD_ROUTE,
-                       GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
-                       size,
-                       (char *) &block,
-                       GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
-                                                 app_announce_time),
-                       app_announce_time,
-                       NULL, NULL);
-    }
-    len = strlen(proof);
-    size = sizeof (struct MeshRegexBlock) + len;
-    block = GNUNET_malloc (size);
-
-    block->key = *key;
-    block->n_proof = htonl (len);
-    block->n_edges = htonl (num_edges);
-    block->accepting = htonl (accepting);
-
-    /* Store the proof at the end of the block. */
-    aux = (char *) &block[1];
-    memcpy (aux, proof, len);
-    aux = &aux[len];
-
-    /* Store each edge in a variable length MeshEdge struct at the
-     * very end of the MeshRegexBlock structure.
-     */
-    for (i = 0; i < num_edges; i++)
-    {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "    edge %s towards %s\n",
-                    edges[i].label,
-                    GNUNET_h2s(&edges[i].destination));
-
-        /* aux points at the end of the last block */
-        len = strlen (edges[i].label);
-        size += sizeof (struct MeshRegexEdge) + len;
-        // Calculate offset FIXME is this ok? use size instead?
-        offset = aux - (char *) block;
-        block = GNUNET_realloc (block, size);
-        aux = &((char *) block)[offset];
-        block_edge = (struct MeshRegexEdge *) aux;
-        block_edge->key = edges[i].destination;
-        block_edge->n_token = htonl (len);
-        aux = (char *) &block_edge[1];
-        memcpy (aux, edges[i].label, len);
-        aux = &aux[len];
-    }
-    (void)
-    GNUNET_DHT_put(dht_handle, key,
-                   dht_replication_level,
-                   opt,
-                   GNUNET_BLOCK_TYPE_MESH_REGEX, size,
-                   (char *) block,
-                   GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
-                                            app_announce_time),
-                   app_announce_time,
-                   NULL, NULL);
-    GNUNET_free (block);
-}
-
-
-/**
- * Store the regular expression describing a local service into the DHT.
- *
- * @param regex The regular expresion.
- */
-static void
-regex_put (const char *regex)
-{
-  struct GNUNET_REGEX_Automaton *dfa;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
-  dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
-  GNUNET_REGEX_iterate_all_edges (dfa, &regex_iterator, NULL);
-  GNUNET_REGEX_automaton_destroy (dfa);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
+queue_send (void *cls, size_t size, void *buf);
 
-}
+/******************************************************************************/
+/************************    REGEX INTEGRATION     ****************************/
+/******************************************************************************/
 
 /**
- * Find a path to a peer that offers a regex servcie compatible
- * with a given string.
- * 
- * @param key The key of the accepting state.
- * @param ctx Context containing info about the string, tunnel, etc.
+ * Cancel a mesh regex search and free resources.
  */
 static void
-regex_find_path (const struct GNUNET_HashCode *key,
-                 struct MeshRegexSearchContext *ctx)
+regex_cancel_search (struct MeshRegexSearchInfo *regex_search)
 {
-  struct GNUNET_DHT_GetHandle *get_h;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
-  get_h = GNUNET_DHT_get_start (dht_handle,    /* handle */
-                                GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
-                                key,     /* key to search */
-                                dht_replication_level, /* replication level */
-                                GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
-                                GNUNET_DHT_RO_RECORD_ROUTE,
-                                NULL,       /* xquery */ // FIXME BLOOMFILTER
-                                0,     /* xquery bits */ // FIXME BLOOMFILTER SIZE
-                                &dht_get_string_accept_handler, ctx);
-  GNUNET_break (GNUNET_OK ==
-                GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
-                                                  key,
-                                                  get_h,
-                                                  GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
+  DEBUG_REGEX ("Search for %s canelled.\n", regex_search->description);
+  GNUNET_REGEX_search_cancel (regex_search->search_handle);
+  if (0 < regex_search->n_peers)
+    GNUNET_free (regex_search->peers);
+  if (GNUNET_SCHEDULER_NO_TASK != regex_search->timeout)
+  {
+    GNUNET_SCHEDULER_cancel(regex_search->timeout);
+  }
+  GNUNET_free (regex_search);
 }
 
 
@@ -1616,15 +1285,16 @@ regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_PEER_Id id;
   GNUNET_PEER_Id old;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
+  DEBUG_REGEX ("Regex connect timeout\n");
   info->timeout = GNUNET_SCHEDULER_NO_TASK;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
+    DEBUG_REGEX (" due to shutdown\n");
     return;
   }
 
   old = info->peer;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  timed out: %u\n", old);
+  DEBUG_REGEX ("  timed out: %u\n", old);
 
   if (0 < info->n_peers)
   {
@@ -1638,7 +1308,7 @@ regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     // Try to connect to same peer again.
     id = info->peer;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  trying: %u\n", id);
+  DEBUG_REGEX ("  trying: %u\n", id);
 
   peer_info = peer_info_get_short(id);
   tunnel_add_peer (info->t, peer_info);
@@ -1648,42 +1318,114 @@ regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
                                                 &regex_connect_timeout,
                                                 info);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
+  DEBUG_REGEX ("Regex connect timeout END\n");
 }
 
 
 /**
- * Cancel an ongoing regex search in the DHT and free all resources.
+ * Function to process DHT string to regex matching.
+ * Called on each result obtained for the DHT search.
  *
- * @param ctx The search context.
+ * @param cls Closure provided in GNUNET_REGEX_search.
+ * @param id Peer providing a regex that matches the string.
+ * @param get_path Path of the get request.
+ * @param get_path_length Lenght of get_path.
+ * @param put_path Path of the put request.
+ * @param put_path_length Length of the put_path.
  */
 static void
-regex_cancel_search(struct MeshRegexSearchContext *ctx)
+regex_found_handler (void *cls,
+                     const struct GNUNET_PeerIdentity *id,
+                     const struct GNUNET_PeerIdentity *get_path,
+                     unsigned int get_path_length,
+                     const struct GNUNET_PeerIdentity *put_path,
+                     unsigned int put_path_length)
 {
-  struct MeshRegexSearchInfo *info = ctx->info;
-  int i;
+  struct MeshRegexSearchInfo *info = cls;
+  struct MeshPeerPath *p;
+  struct MeshPeerInfo *peer_info;
+
+  DEBUG_REGEX ("Got regex results from DHT!\n");
+  DEBUG_REGEX ("  for %s\n", info->description);
+
+  peer_info = peer_info_get (id);
+  p = path_build_from_dht (get_path, get_path_length,
+                           put_path, put_path_length);
+  path_add_to_peers (p, GNUNET_NO);
+  path_destroy(p);
 
-  GNUNET_free (info->description);
-  GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
-                                             &regex_cancel_dht_get, NULL);
-  GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
-                                         &regex_free_result, NULL);
-  GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
-  GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
-  info->t->regex_ctx = NULL;
-  for (i = 0; i < info->n_contexts; i++)
+  tunnel_add_peer (info->t, peer_info);
+  peer_info_connect (peer_info, info->t);
+  if (0 == info->peer)
   {
-    GNUNET_free (info->contexts[i]);
+    info->peer = peer_info->id;
   }
-  if (0 < info->n_contexts)
-    GNUNET_free (info->contexts);
-  if (0 < info->n_peers)
-    GNUNET_free (info->peers);
+  else
+  {
+    GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
+  }
+
   if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
+    return;
+
+  info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
+                                                &regex_connect_timeout,
+                                                info);
+
+  return;
+}
+
+
+/**
+ * Store the regular expression describing a local service into the DHT.
+ *
+ * @param regex The regular expresion.
+ */
+static void
+regex_put (struct MeshRegexDescriptor *regex)
+{
+  DEBUG_REGEX ("  regex_put (%s) start\n", regex->regex);
+  if (NULL == regex->h)
   {
-    GNUNET_SCHEDULER_cancel(info->timeout);
+    DEBUG_REGEX ("  first put, creating DFA\n");
+    regex->h = GNUNET_REGEX_announce (dht_handle,
+                                      &my_full_id,
+                                      regex->regex,
+                                      regex->compression,
+                                      stats);
   }
-  GNUNET_free (info);
+  else
+  {
+    DEBUG_REGEX ("  not first put, using cached data\n");
+    GNUNET_REGEX_reannounce (regex->h);
+  }
+  DEBUG_REGEX ("  regex_put (%s) end\n", regex->regex);
+}
+
+
+/**
+ * Periodically announce what applications are provided by local clients
+ * (by regex)
+ *
+ * @param cls closure
+ * @param tc task context
+ */
+static void
+regex_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct MeshClient *c = cls;
+  unsigned int i;
+
+  c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+  DEBUG_REGEX ("Starting announce for regex\n");
+  for (i = 0; i < c->n_regex; i++)
+    regex_put (&c->regexes[i]);
+  c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
+                                                         &regex_announce,
+                                                         cls);
+  DEBUG_REGEX ("Finished announce for regex\n");
 }
 
 
@@ -1717,55 +1459,21 @@ announce_application (void *cls, const struct GNUNET_HashCode * key, void *value
     return GNUNET_YES;
   }
   block.type = htonl (block.type);
-
+  DEBUG_DHT ("Putting APP key: %s\n", GNUNET_h2s (key));
   GNUNET_break (NULL != 
                 GNUNET_DHT_put (dht_handle, key,
-                  dht_replication_level,
-                  GNUNET_DHT_RO_RECORD_ROUTE |
-                  GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
-                  GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
-                  sizeof (block),
-                  (const char *) &block,
-                  GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
-                                            app_announce_time),
-                  app_announce_time, NULL, NULL));
+                               dht_replication_level,
+                               GNUNET_DHT_RO_RECORD_ROUTE |
+                               GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
+                               GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
+                               sizeof (block),
+                               (const char *) &block,
+                               GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
+                               app_announce_time, NULL, NULL));
   return GNUNET_OK;
 }
 
 
-/**
- * Periodically announce what applications are provided by local clients
- * (by regex)
- *
- * @param cls closure
- * @param tc task context
- */
-static void
-announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct MeshClient *c = cls;
-  unsigned int i;
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
-    return;
-  }
-
-  DEBUG_DHT ("Starting PUT for regex\n");
-
-  for (i = 0; i < c->n_regex; i++)
-  {
-    regex_put (c->regexes[i]);
-  }
-  c->regex_announce_task =
-      GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
-  DEBUG_DHT ("Finished PUT for regex\n");
-
-  return;
-}
-
-
 /**
  * Periodically announce what applications are provided by local clients
  * (by type)
@@ -1790,8 +1498,6 @@ announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
                                     cls);
   DEBUG_DHT ("Finished PUT for apps\n");
-
-  return;
 }
 
 
@@ -2012,6 +1718,32 @@ client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
 }
 
 
+/**
+ * Notify the owner of a tunnel that a peer has disconnected.
+ * 
+ * @param c Client (owner of tunnel).
+ * @param t Tunnel this message is about.
+ * @param peer_id Short ID of the disconnected peer.
+ */
+void
+client_notify_peer_disconnected (struct MeshClient *c,
+                                 struct MeshTunnel *t,
+                                 GNUNET_PEER_Id peer_id)
+{
+  struct GNUNET_MESH_PeerControl msg;
+
+  if (NULL == t->owner || NULL == nc)
+    return;
+
+  msg.header.size = htons (sizeof (msg));
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
+  msg.tunnel_id = htonl (t->local_tid);
+  GNUNET_PEER_resolve (peer_id, &msg.peer);
+  GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
+                                              &msg.header, GNUNET_NO);
+}
+
+
 /**
  * Send the message to all clients that have subscribed to its type
  *
@@ -2129,6 +1861,9 @@ send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
 {
   struct GNUNET_MESH_PeerControl pc;
 
+  if (NULL == t->owner || GNUNET_YES == t->destroy)
+    return;
+
   pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
   pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
   pc.tunnel_id = htonl (t->local_tid);
@@ -2198,6 +1933,23 @@ send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
 }
 
 
+/**
+ * Iterator over all the peers to remove the oldest not-used entry.
+ *
+ * @param cls Closure (unsued).
+ * @param key ID of the peer.
+ * @param value Peer_Info of the peer.
+ *
+ * FIXME implement
+ */
+static int
+peer_info_timeout (void *cls,
+                   const struct GNUNET_HashCode *key,
+                   void *value)
+{
+  return GNUNET_YES;
+}
+
 /**
  * Retrieve the MeshPeerInfo stucture associated with the peer, create one
  * and insert it in the appropiate structures if the peer is not known yet.
@@ -2216,10 +1968,17 @@ peer_info_get (const struct GNUNET_PeerIdentity *peer)
   {
     peer_info =
         (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
+    if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
+    {
+      GNUNET_CONTAINER_multihashmap_iterate (peers,
+                                             &peer_info_timeout,
+                                             NULL);
+    }
     GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
-                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
     peer_info->id = GNUNET_PEER_intern (peer);
   }
+  peer_info->last_contact = GNUNET_TIME_absolute_get();
 
   return peer_info;
 }
@@ -2267,7 +2026,9 @@ peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *va
     {
       peer->ntunnels--;
       peer->tunnels[i] = peer->tunnels[peer->ntunnels];
-      peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
+      peer->tunnels = 
+        GNUNET_realloc (peer->tunnels, 
+                        peer->ntunnels * sizeof(struct MeshTunnel *));
       return GNUNET_YES;
     }
   }
@@ -2335,12 +2096,19 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
   info->mesh_data->data = GNUNET_malloc (size);
   memcpy (info->mesh_data->data, message, size);
   type = ntohs(message->type);
-  if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
+  switch (type)
   {
     struct GNUNET_MESH_Unicast *m;
+    struct GNUNET_MESH_ToOrigin *to;
 
-    m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
-    m->ttl = htonl (ntohl (m->ttl) - 1);
+    case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+      m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
+      m->ttl = htonl (ntohl (m->ttl) - 1);
+      break;
+    case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+      to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
+      t->bck_pid++;
+      to->pid = htonl(t->bck_pid);
   }
   info->mesh_data->data_len = size;
   info->mesh_data->reference_counter = 1;
@@ -2380,6 +2148,9 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
     }
 #endif
     GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                    " no direct connection to %s\n",
+                    GNUNET_i2s (peer));
     GNUNET_free (info->mesh_data->data);
     GNUNET_free (info->mesh_data);
     GNUNET_free (info);
@@ -2603,6 +2374,8 @@ peer_info_connect_task (void *cls,
 {
   struct MeshPathInfo *path_info = cls;
 
+  path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
+
   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
   {
     GNUNET_free (cls);
@@ -2650,6 +2423,10 @@ peer_info_destroy (struct MeshPeerInfo *pi)
     path_destroy (p);
     p = nextp;
   }
+  if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
+  {
+    GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
+  }
   GNUNET_free (pi);
   return GNUNET_OK;
 }
@@ -2855,6 +2632,44 @@ peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
 }
 
 
+/**
+ * Function called if the connection to the peer has been stalled for a while,
+ * possibly due to a missed ACK. Poll the peer about its ACK status.
+ *
+ * @param cls Closure (cinfo).
+ * @param tc TaskContext.
+ */
+static void
+tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct MeshTunnelChildInfo *cinfo = cls;
+  struct GNUNET_MESH_Poll msg;
+  struct GNUNET_PeerIdentity id;
+  struct MeshTunnel *t;
+
+  cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+  {
+    return;
+  }
+
+  t = cinfo->t;
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
+  msg.header.size = htons (sizeof (msg));
+  msg.tid = htonl (t->id.tid);
+  GNUNET_PEER_resolve (t->id.oid, &msg.oid);
+  msg.last_ack = htonl (cinfo->fwd_ack);
+
+  GNUNET_PEER_resolve (cinfo->id, &id);
+  send_prebuilt_message (&msg.header, &id, cinfo->t);
+  cinfo->fc_poll_time = GNUNET_TIME_relative_min (
+    MESH_MAX_POLL_TIME,
+    GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
+  cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
+                                                 &tunnel_poll, cinfo);
+}
+
+
 /**
  * Build a PeerPath from the paths returned from the DHT, reversing the paths
  * to obtain a local peer -> destination path and interning the peer ids.
@@ -3054,7 +2869,7 @@ tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
  * @return tunnel handler, NULL if doesn't exist
  */
 static struct MeshTunnel *
-tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
+tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
 {
   return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
 }
@@ -3143,19 +2958,27 @@ tunnel_destroy_child (void *cls,
 {
   struct MeshTunnelChildInfo *cinfo = value;
   struct MeshTunnel *t = cls;
+  struct MeshPeerQueue *q;
   unsigned int c;
   unsigned int i;
 
   for (c = 0; c < cinfo->send_buffer_n; c++)
   {
     i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
-    if (NULL != cinfo->send_buffer[i])
-      queue_destroy (cinfo->send_buffer[i], GNUNET_YES);
+    q = cinfo->send_buffer[i];
+    cinfo->send_buffer[i] = NULL;
+    if (NULL != q)
+      queue_destroy (q, GNUNET_YES);
     else
       GNUNET_break (0);
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
   }
   GNUNET_free_non_null (cinfo->send_buffer);
+  if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
+  {
+    GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
+    cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
+  }
   GNUNET_free (cinfo);
   return GNUNET_YES;
 }
@@ -3175,22 +2998,14 @@ tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
   struct MeshPeerInfo *peer;
   struct MeshPathInfo *path_info;
 
-  if (NULL != t->owner && NULL != nc)
-  {
-    struct GNUNET_MESH_PeerControl msg;
+  client_notify_peer_disconnected (t->owner, t, peer_id);
 
-    msg.header.size = htons (sizeof (msg));
-    msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
-    msg.tunnel_id = htonl (t->local_tid);
-    GNUNET_PEER_resolve (peer_id, &msg.peer);
-    GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
-                                                &msg.header, GNUNET_NO);
-  }
   peer = peer_info_get_short (peer_id);
   path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
   path_info->peer = peer;
   path_info->t = t;
-  GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
+  peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
+                                                 path_info);
 }
 
 
@@ -3217,7 +3032,6 @@ tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
   if (GNUNET_NO ==
       GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
   {
-    t->peers_total++;
     GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
     GNUNET_assert (GNUNET_OK ==
                    GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
@@ -3438,7 +3252,8 @@ tunnel_send_multicast (struct MeshTunnel *t,
               "  no one to send data to\n");
     GNUNET_free (mdata->data);
     GNUNET_free (mdata);
-    t->fwd_queue_n--;
+    if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
+      t->fwd_queue_n--;
   }
   else
   {
@@ -3493,7 +3308,7 @@ tunnel_add_skip (void *cls,
  * @return Neighbor's Flow Control info.
  */
 static struct MeshTunnelChildInfo *
-tunnel_get_neighbor_fc (const struct MeshTunnel *t,
+tunnel_get_neighbor_fc (struct MeshTunnel *t,
                         const struct GNUNET_PeerIdentity *peer)
 {
   struct MeshTunnelChildInfo *cinfo;
@@ -3510,10 +3325,15 @@ tunnel_get_neighbor_fc (const struct MeshTunnel *t,
     cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
     cinfo->id = GNUNET_PEER_intern (peer);
     cinfo->skip = t->fwd_pid;
+    cinfo->t = t;
 
     delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
     cinfo->fwd_ack = t->fwd_pid + delta;
     cinfo->bck_ack = delta;
+    cinfo->bck_pid = -1;
+
+    cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
+    cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
 
     cinfo->send_buffer =
         GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
@@ -3730,7 +3550,12 @@ tunnel_get_fwd_ack (struct MeshTunnel *t)
   if (-1LL == child_ack)
   {
     // Node has no children, child_ack AND core buffer are irrelevant.
-    GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
+    if (-1LL == client_ack) // No children AND no clients? Not good!
+    {
+      GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
+                                1, GNUNET_NO);
+
+    }
     return (uint32_t) client_ack;
   }
   if (-1LL == client_ack)
@@ -3866,13 +3691,17 @@ tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
     case GNUNET_MESSAGE_TYPE_MESH_ACK:
     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
       break;
+    case GNUNET_MESSAGE_TYPE_MESH_POLL:
+      t->force_ack = GNUNET_YES;
+      break;
     default:
       GNUNET_break (0);
   }
 
-  /* Check if we need no retransmit the ACK */
+  /* Check if we need to transmit the ACK */
   if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
-      GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
+      GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
+      GNUNET_NO == t->force_ack)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -3888,7 +3717,7 @@ tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
   ack = tunnel_get_fwd_ack (t);
 
   /* If speed_min and not all children have ack'd, dont send yet */
-  if (ack == t->last_fwd_ack)
+  if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
     return;
@@ -3898,6 +3727,7 @@ tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
   GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
   send_ack (t, &id, ack);
   debug_fwd_ack++;
+  t->force_ack = GNUNET_NO;
 }
 
 
@@ -3915,16 +3745,24 @@ tunnel_send_child_bck_ack (void *cls,
   struct MeshTunnel *t = cls;
   struct MeshTunnelChildInfo *cinfo;
   struct GNUNET_PeerIdentity peer;
+  uint32_t ack;
 
   GNUNET_PEER_resolve (id, &peer);
   cinfo = tunnel_get_neighbor_fc (t, &peer);
+  ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
 
-  if (cinfo->bck_ack != cinfo->pid &&
-      GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
+  if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "    Not sending ACK, not needed\n");
     return;
+  }
+  cinfo->bck_ack = ack;
 
-  cinfo->bck_ack++;
-  send_ack (t, &peer, cinfo->bck_ack);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "    Sending BCK ACK %u (last sent: %u)\n",
+              ack, cinfo->bck_ack);
+  send_ack (t, &peer, ack);
 }
 
 
@@ -3932,6 +3770,14 @@ tunnel_send_child_bck_ack (void *cls,
  * @brief Send BCK ACKs to clients to allow them more to_origin traffic
  * 
  * Iterates over all clients and sends BCK ACKs to the ones that need it.
+ *
+ * FIXME fc: what happens if we have 2 clients but q_size is 1?
+ *           - implement a size 1 buffer in each client_fc AND children_fc
+ *           to hold at least 1 message per "child".
+ *             problem: violates no buffer policy
+ *           - ack 0 and make "children" poll for transmission slots
+ *             problem: big overhead, extra latency even in low traffic
+ *                      settings
  * 
  * @param t Tunnel on which to send the BCK ACKs.
  */
@@ -3943,7 +3789,7 @@ tunnel_send_clients_bck_ack (struct MeshTunnel *t)
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Sending BCK ACK to clients\n");
 
-  tunnel_delta = t->bck_ack - t->bck_pid;
+  tunnel_delta = t->bck_queue_max - t->bck_queue_n;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   tunnel delta: %u\n", tunnel_delta);
 
   /* Find client whom to allow to send to origin (with lowest buffer space) */
@@ -3970,6 +3816,12 @@ tunnel_send_clients_bck_ack (struct MeshTunnel *t)
       send_local_ack (t, t->clients[i], ack);
       clinfo->bck_ack = ack;
     }
+    else
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "    not sending ack to client %u (td %u, d %u)\n",
+                  t->clients[i]->id, tunnel_delta, delta);
+    }
   }
 }
 
@@ -4005,12 +3857,16 @@ tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
     case GNUNET_MESSAGE_TYPE_MESH_ACK:
     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
       break;
+    case GNUNET_MESSAGE_TYPE_MESH_POLL:
+      t->force_ack = GNUNET_YES;
+      break;
     default:
       GNUNET_break (0);
   }
 
   tunnel_send_clients_bck_ack (t);
   tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
+  t->force_ack = GNUNET_NO;
 }
 
 
@@ -4102,17 +3958,37 @@ tunnel_unlock_bck_queue (struct MeshTunnel *t)
  * valid.
  *
  * @param t The tunnel whose peers to notify.
+ * @param parent ID of the parent, in case the tree is already destroyed.
  */
 static void
-tunnel_send_destroy (struct MeshTunnel *t)
+tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
 {
   struct GNUNET_MESH_TunnelDestroy msg;
+  struct GNUNET_PeerIdentity id;
 
   msg.header.size = htons (sizeof (msg));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
   GNUNET_PEER_resolve (t->id.oid, &msg.oid);
   msg.tid = htonl (t->id.tid);
-  tunnel_send_multicast (t, &msg.header);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "  sending tunnel destroy for tunnel: %s [%X]\n",
+              GNUNET_i2s (&msg.oid), t->id.tid);
+  if (tree_count_children(t->tree) > 0)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  sending multicast to children\n");
+    tunnel_send_multicast (t, &msg.header);
+  }
+  if (0 == parent)
+    parent = tree_get_predecessor (t->tree);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  parent: %u\n", parent);
+  if (0 == parent)
+    return;
+
+  GNUNET_PEER_resolve (parent, &id);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "  sending back to %s\n",
+              GNUNET_i2s (&id));
+  send_prebuilt_message (&msg.header, &id, t);
 }
 
 
@@ -4225,17 +4101,10 @@ tunnel_destroy (struct MeshTunnel *t)
     }
   }
 
-  if (t->nclients > 0)
-  {
-    if (GNUNET_YES !=
-        GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
-    {
-      GNUNET_break (0);
-      r = GNUNET_SYSERR;
-    }
-    GNUNET_free (t->clients);
-    GNUNET_free (t->clients_fc);
-  }
+  (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
+  GNUNET_free_non_null (t->clients);
+  GNUNET_free_non_null (t->ignore);
+  GNUNET_free_non_null (t->clients_fc);
 
   if (NULL != t->peers)
   {
@@ -4244,28 +4113,104 @@ tunnel_destroy (struct MeshTunnel *t)
     GNUNET_CONTAINER_multihashmap_destroy (t->peers);
   }
 
-  GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
-                                         &tunnel_destroy_child,
-                                         t);
-  GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
-  t->children_fc = NULL;
-
-  tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
-  tree_destroy (t->tree);
+  GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
+                                         &tunnel_destroy_child,
+                                         t);
+  GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
+  t->children_fc = NULL;
+
+  tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
+  tree_destroy (t->tree);
+
+  if (NULL != t->regex_search)
+    GNUNET_REGEX_search_cancel (t->regex_search->search_handle);
+  if (NULL != t->dht_get_type)
+    GNUNET_DHT_get_stop (t->dht_get_type);
+  if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
+    GNUNET_SCHEDULER_cancel (t->timeout_task);
+  if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
+    GNUNET_SCHEDULER_cancel (t->path_refresh_task);
+
+  n_tunnels--;
+  GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
+  GNUNET_free (t);
+  return r;
+}
+
+#define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
+
+/**
+ * Tunnel is empty: destroy it.
+ * 
+ * @param cls Closure (Tunnel).
+ * @param tc TaskContext. 
+ */
+static void
+tunnel_destroy_empty_delayed (void *cls,
+                              const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct MeshTunnel *t = cls;
+
+  t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+
+  if (0 != t->nclients ||
+      0 != tree_count_children (t->tree))
+    return;
+
+  #if MESH_DEBUG
+  {
+    struct GNUNET_PeerIdentity id;
+
+    GNUNET_PEER_resolve (t->id.oid, &id);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "executing destruction of empty tunnel %s [%X]\n",
+                GNUNET_i2s (&id), t->id.tid);
+  }
+  #endif
+
+  tunnel_send_destroy (t, 0);
+  if (0 == t->pending_messages)
+    tunnel_destroy (t);
+  else
+    t->destroy = GNUNET_YES;
+}
+
+
+/**
+ * Schedule tunnel destruction if is empty and no new traffic comes in a time.
+ * 
+ * @param t Tunnel to destroy if empty.
+ */
+static void
+tunnel_destroy_empty (struct MeshTunnel *t)
+{
+  if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy || 
+      0 != t->nclients ||
+      0 != tree_count_children (t->tree))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "%u %u %u\n",
+                t->delayed_destroy, t->nclients, tree_count_children(t->tree));
+    return;
+  }
+
+  #if MESH_DEBUG
+  {
+    struct GNUNET_PeerIdentity id;
 
-  if (NULL != t->regex_ctx)
-    regex_cancel_search (t->regex_ctx);
-  if (NULL != t->dht_get_type)
-    GNUNET_DHT_get_stop (t->dht_get_type);
-  if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
-    GNUNET_SCHEDULER_cancel (t->timeout_task);
-  if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
-    GNUNET_SCHEDULER_cancel (t->path_refresh_task);
+    GNUNET_PEER_resolve (t->id.oid, &id);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "scheduling destruction of empty tunnel %s [%X]\n",
+                GNUNET_i2s (&id), t->id.tid);
+  }
+  #endif
 
-  n_tunnels--;
-  GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
-  GNUNET_free (t);
-  return r;
+  t->delayed_destroy =
+      GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
+                                    &tunnel_destroy_empty_delayed,
+                                    t);
 }
 
 
@@ -4303,7 +4248,7 @@ tunnel_new (GNUNET_PEER_Id owner,
   t->bck_ack = INITIAL_WINDOW_SIZE - 1;
   t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
   t->local_tid = local;
-  t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
+  t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
   n_tunnels++;
   GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
 
@@ -4339,6 +4284,19 @@ tunnel_new (GNUNET_PEER_Id owner,
   return t;
 }
 
+/**
+ * Callback when removing children from a tunnel tree. Notify owner.
+ *
+ * @param cls Closure (tunnel).
+ * @param peer_id Short ID of the peer deleted.
+ */
+void
+tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
+{
+  struct MeshTunnel *t = cls;
+
+  client_notify_peer_disconnected (t->owner, t, peer_id);
+}
 
 /**
  * Removes an explicit path from a tunnel, freeing all intermediate nodes
@@ -4351,8 +4309,15 @@ tunnel_new (GNUNET_PEER_Id owner,
 static void
 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
 {
-  if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
-    tunnel_destroy (t);
+  int r;
+
+  r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
+  if (GNUNET_NO == r)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Tunnel %u [%u] has no more nodes\n",
+                t->id.oid, t->id.tid);
+  }
 }
 
 
@@ -4369,21 +4334,23 @@ tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
  * @return GNUNET_OK, keep iterating.
  */
 static int
-tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
+tunnel_destroy_iterator (void *cls,
+                         const struct GNUNET_HashCode * key,
+                         void *value)
 {
   struct MeshTunnel *t = value;
   struct MeshClient *c = cls;
 
-  send_client_tunnel_disconnect(t, c);
+  send_client_tunnel_disconnect (t, c);
   if (c != t->owner)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Client %u is destination, keeping the tunnel alive.\n", c->id);
-    tunnel_delete_client(t, c);
-    client_delete_tunnel(c, t);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
+    tunnel_delete_client (t, c);
+    client_delete_tunnel (c, t);
+    tunnel_destroy_empty (t);
     return GNUNET_OK;
   }
-  tunnel_send_destroy(t);
+  tunnel_send_destroy (t, 0);
   t->owner = NULL;
   t->destroy = GNUNET_YES;
 
@@ -4609,13 +4576,20 @@ queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
     switch (queue->type)
     {
       case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "   cancelling TUNNEL_DESTROY\n");
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "   cancelling TUNNEL_DESTROY\n");
+        GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
         /* fall through */
       case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
       case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
       case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+      case GNUNET_MESSAGE_TYPE_MESH_ACK:
+      case GNUNET_MESSAGE_TYPE_MESH_POLL:
+      case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                    "   prebuilt message\n");
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "   type prebuilt (payload, tunnel destroy)\n");
+                    "   type %s\n",
+                    GNUNET_MESH_DEBUG_M2S(queue->type));
         dd = queue->cls;
         data_descriptor_decrement_rc (dd->mesh_data);
         break;
@@ -4776,6 +4750,7 @@ queue_send (void *cls, size_t size, void *buf)
     size_t data_size;
 
     peer->core_transmit = NULL;
+    cinfo = NULL;
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
     queue = queue_get_next (peer);
@@ -4791,6 +4766,9 @@ queue_send (void *cls, size_t size, void *buf)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   not empty\n");
 
     GNUNET_PEER_resolve (peer->id, &dst_id);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "*********   towards %s\n",
+                GNUNET_i2s(&dst_id));
     /* Check if buffer size is enough for the message */
     if (queue->size > size)
     {
@@ -4810,6 +4788,8 @@ queue_send (void *cls, size_t size, void *buf)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   size ok\n");
 
     t = queue->tunnel;
+    GNUNET_assert (0 < t->pending_messages);
+    t->pending_messages--;
     if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
     {
       t->fwd_queue_n--;
@@ -4828,6 +4808,7 @@ queue_send (void *cls, size_t size, void *buf)
     {
       case 0:
       case GNUNET_MESSAGE_TYPE_MESH_ACK:
+      case GNUNET_MESSAGE_TYPE_MESH_POLL:
       case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
       case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
       case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
@@ -4866,6 +4847,12 @@ queue_send (void *cls, size_t size, void *buf)
                         "*********   considered sent\n");
             t->fwd_queue_n--;
           }
+          else
+          {
+            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                        "*********   NOT considered sent yet\n");
+            t->pending_messages++;
+          }
         }
         data_size = send_core_data_multicast(queue->cls, size, buf);
         tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
@@ -4884,7 +4871,7 @@ queue_send (void *cls, size_t size, void *buf)
         break;
       default:
         GNUNET_break (0);
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                     "*********   type unknown: %u\n",
                     queue->type);
         data_size = 0;
@@ -4923,9 +4910,8 @@ queue_send (void *cls, size_t size, void *buf)
     /* Free queue, but cls was freed by send_core_* */
     queue_destroy (queue, GNUNET_NO);
 
-    if (GNUNET_YES == t->destroy)
+    if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
     {
-      // FIXME fc tunnel destroy all pending traffic? wait for it?
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********  destroying tunnel!\n");
       tunnel_destroy (t);
     }
@@ -4951,9 +4937,19 @@ queue_send (void *cls, size_t size, void *buf)
     else
     {
       if (NULL != peer->queue_head)
-        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                     "*********   %s stalled\n",
                     GNUNET_i2s(&my_full_id));
+        if (NULL == cinfo)
+          cinfo = tunnel_get_neighbor_fc (t, &dst_id);
+        // FIXME unify bck/fwd structures, bck does not have cinfo right now
+        if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
+        {
+          cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
+                                                         &tunnel_poll, cinfo);
+        }
+      }
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   return %d\n", data_size);
     return data_size;
@@ -5000,16 +4996,15 @@ queue_add (void *cls, uint16_t type, size_t size,
     n = &t->bck_queue_n;
     max = &t->bck_queue_max;
   }
-  if (NULL != n) {
+  if (NULL != n)
+  {
     if (*n >= *max)
     {
-      if (NULL == t->owner)
-        GNUNET_break_op(0);       // TODO: kill connection?
-      else
-        GNUNET_break(0);
-      GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
+      GNUNET_break(0);
+      GNUNET_STATISTICS_update(stats,
+                               "# messages dropped (buffer full)",
                                1, GNUNET_NO);
-      return;                       // Drop message
+      return; // Drop message
     }
     (*n)++;
   }
@@ -5033,6 +5028,7 @@ queue_add (void *cls, uint16_t type, size_t size,
                                              &queue_send,
                                              dst);
   }
+  t->pending_messages++;
   if (NULL == n) // Is this internal mesh traffic?
     return;
 
@@ -5070,17 +5066,13 @@ queue_add (void *cls, uint16_t type, size_t size,
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
-                         const struct GNUNET_MessageHeader *message,
-                         const struct GNUNET_ATS_Information *atsi,
-                         unsigned int atsi_count)
+                         const struct GNUNET_MessageHeader *message)
 {
   unsigned int own_pos;
   uint16_t size;
@@ -5224,7 +5216,7 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
     if (NULL == t->peers)
     {
       /* New tunnel! Notify clients on first payload message. */
-      t->peers = GNUNET_CONTAINER_multihashmap_create (4);
+      t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
     }
     GNUNET_break (GNUNET_SYSERR !=
                   GNUNET_CONTAINER_multihashmap_put (t->peers,
@@ -5256,17 +5248,13 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
-                          const struct GNUNET_MessageHeader *message,
-                          const struct GNUNET_ATS_Information *atsi,
-                          unsigned int atsi_count)
+                          const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_ManipulatePath *msg;
   struct GNUNET_PeerIdentity *pi;
@@ -5340,17 +5328,13 @@ handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
-                         const struct GNUNET_MessageHeader *message,
-                         const struct GNUNET_ATS_Information *atsi,
-                         unsigned int atsi_count)
+                         const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_PathBroken *msg;
   struct MeshTunnel *t;
@@ -5381,40 +5365,53 @@ handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
-                            const struct GNUNET_MessageHeader *message,
-                            const struct GNUNET_ATS_Information *atsi,
-                            unsigned int atsi_count)
+                            const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_TunnelDestroy *msg;
   struct MeshTunnel *t;
+  GNUNET_PEER_Id parent;
+  GNUNET_PEER_Id pid;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
   msg = (struct GNUNET_MESH_TunnelDestroy *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  for tunnel %s [%u]\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Got a TUNNEL DESTROY packet from %s\n",
+              GNUNET_i2s (peer));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "  for tunnel %s [%u]\n",
               GNUNET_i2s (&msg->oid), ntohl (msg->tid));
   t = tunnel_get (&msg->oid, ntohl (msg->tid));
+  /* Check signature */
   if (NULL == t)
   {
     /* Probably already got the message from another path,
      * destroyed the tunnel and retransmitted to children.
      * Safe to ignore.
      */
-    GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
+    GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
+                              1, GNUNET_NO);
     return GNUNET_OK;
   }
-  if (t->id.oid == myid)
+  parent = tree_get_predecessor (t->tree);
+  pid = GNUNET_PEER_search (peer);
+  if (pid != parent)
   {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
+    unsigned int nc;
+
+    tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
+    nc = tree_count_children (t->tree);
+    if (nc > 0 || NULL != t->owner || t->nclients > 0)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "still in use: %u cl, %u ch\n",
+                  t->nclients, nc);
+      return GNUNET_OK;
+    }
   }
   if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
   {
@@ -5423,7 +5420,7 @@ handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
                 t->local_tid, t->local_tid_dest);
     send_clients_tunnel_destroy (t);
   }
-  tunnel_send_destroy (t);
+  tunnel_send_destroy (t, parent);
   t->destroy = GNUNET_YES;
   // TODO: add timeout to destroy the tunnel anyway
   return GNUNET_OK;
@@ -5436,16 +5433,12 @@ handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param peer peer identity this notification is about
  * @param message message
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
-                          const struct GNUNET_MessageHeader *message,
-                          const struct GNUNET_ATS_Information *atsi,
-                          unsigned int atsi_count)
+                          const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_Unicast *msg;
   struct GNUNET_PeerIdentity *neighbor;
@@ -5531,7 +5524,7 @@ handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
 
   neighbor = tree_get_first_hop (t->tree, dest_id);
   cinfo = tunnel_get_neighbor_fc (t, neighbor);
-  cinfo->pid = pid;
+  cinfo->fwd_pid = pid;
   GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
                                          &tunnel_add_skip,
                                          &neighbor);
@@ -5555,8 +5548,6 @@ handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  *
@@ -5564,9 +5555,7 @@ handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
  */
 static int
 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
-                            const struct GNUNET_MessageHeader *message,
-                            const struct GNUNET_ATS_Information *atsi,
-                            unsigned int atsi_count)
+                            const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_Multicast *msg;
   struct MeshTunnel *t;
@@ -5639,24 +5628,22 @@ handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
-                          const struct GNUNET_MessageHeader *message,
-                          const struct GNUNET_ATS_Information *atsi,
-                          unsigned int atsi_count)
+                          const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_ToOrigin *msg;
   struct GNUNET_PeerIdentity id;
   struct MeshPeerInfo *peer_info;
   struct MeshTunnel *t;
+  struct MeshTunnelChildInfo *cinfo;
+  GNUNET_PEER_Id predecessor;
   size_t size;
-
+  uint32_t pid;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
               GNUNET_i2s (peer));
@@ -5671,15 +5658,39 @@ handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
               GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
   t = tunnel_get (&msg->oid, ntohl (msg->tid));
+  pid = ntohl (msg->pid);
 
   if (NULL == t)
   {
     /* TODO notify that we dont know this tunnel (whom)? */
     GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
-    GNUNET_break_op (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
+                pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
+    return GNUNET_OK;
+  }
+
+  cinfo = tunnel_get_neighbor_fc(t, peer);
+  if (NULL == cinfo)
+  {
+    GNUNET_break (0);
+    return GNUNET_OK;
+  }
+
+  if (cinfo->bck_pid == pid)
+  {
+    /* already seen this packet, drop */
+    GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                " Already seen pid %u, DROPPING!\n", pid);
+    tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
     return GNUNET_OK;
   }
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              " pid %u not seen yet, forwarding\n", pid);
+  cinfo->bck_pid = pid;
+
   if (NULL != t->owner)
   {
     char cbuf[size];
@@ -5691,6 +5702,8 @@ handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
     memcpy (cbuf, message, size);
     copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
     copy->tid = htonl (t->local_tid);
+    t->bck_pid++;
+    copy->pid = htonl (t->bck_pid);
     GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
     GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
                                                 &copy->header, GNUNET_NO);
@@ -5707,7 +5720,31 @@ handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_break (0);
     return GNUNET_OK;
   }
-  GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
+  predecessor = tree_get_predecessor (t->tree);
+  if (0 == predecessor)
+  {
+    if (GNUNET_YES == t->destroy)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "to orig received on a dying tunnel %s [%X]\n",
+                  GNUNET_i2s (&msg->oid), ntohl(msg->tid));
+      return GNUNET_OK;
+    }
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                "unknown to origin at %s\n",
+                GNUNET_i2s (&my_full_id));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                "from peer %s\n",
+                GNUNET_i2s (peer));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                "for tunnel %s [%X]\n",
+                GNUNET_i2s (&msg->oid), ntohl(msg->tid));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                "current tree:\n");
+    tree_debug (t->tree);
+    return GNUNET_OK;
+  }
+  GNUNET_PEER_resolve (predecessor, &id);
   send_prebuilt_message (message, &id, t);
   GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
 
@@ -5721,17 +5758,13 @@ handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
-                 const struct GNUNET_MessageHeader *message,
-                 const struct GNUNET_ATS_Information *atsi,
-                 unsigned int atsi_count)
+                 const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_ACK *msg;
   struct MeshTunnel *t;
@@ -5747,10 +5780,10 @@ handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
   {
     /* TODO notify that we dont know this tunnel (whom)? */
     GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
-    GNUNET_break_op (0);
     return GNUNET_OK;
   }
   ack = ntohl (msg->pid);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ACK %u\n", ack);
 
   /* Is this a forward or backward ACK? */
   if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
@@ -5763,6 +5796,12 @@ handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
     cinfo->fwd_ack = ack;
     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
     tunnel_unlock_fwd_queues (t);
+    if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
+    {
+      GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
+      cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
+      cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
+    }
   }
   else
   {
@@ -5775,23 +5814,70 @@ handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
 }
 
 
+/**
+ * Core handler for mesh network traffic point-to-point ack polls.
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ *
+ * @return GNUNET_OK to keep the connection open,
+ *         GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
+                  const struct GNUNET_MessageHeader *message)
+{
+  struct GNUNET_MESH_Poll *msg;
+  struct MeshTunnel *t;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
+              GNUNET_i2s (peer));
+
+  msg = (struct GNUNET_MESH_Poll *) message;
+
+  t = tunnel_get (&msg->oid, ntohl (msg->tid));
+
+  if (NULL == t)
+  {
+    /* TODO notify that we dont know this tunnel (whom)? */
+    GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
+    GNUNET_break_op (0);
+    return GNUNET_OK;
+  }
+
+  /* Is this a forward or backward ACK? */
+  if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
+  {
+    struct MeshTunnelChildInfo *cinfo;
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  from FWD\n");
+    cinfo = tunnel_get_neighbor_fc (t, peer);
+    cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
+    tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  from BCK\n");
+    tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
+  }
+
+  return GNUNET_OK;
+}
+
 /**
  * Core handler for path ACKs
  *
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
-                      const struct GNUNET_MessageHeader *message,
-                      const struct GNUNET_ATS_Information *atsi,
-                      unsigned int atsi_count)
+                      const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_PathACK *msg;
   struct GNUNET_PeerIdentity id;
@@ -5820,12 +5906,12 @@ handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  via peer %s\n",
               GNUNET_i2s (peer));
 
-  if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
+  if (NULL != t->regex_search && t->regex_search->peer == peer_info->id)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "connect_by_string completed, stopping search\n");
-    regex_cancel_search (t->regex_ctx);
-    t->regex_ctx = NULL;
+    regex_cancel_search (t->regex_search);
+    t->regex_search = NULL;
   }
 
   /* Add paths to peers? */
@@ -5859,6 +5945,11 @@ handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
       tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
       send_client_peer_connected (t, peer_info->id);
     }
+    if (NULL != peer_info->dhtget)
+    {
+      GNUNET_DHT_get_stop (peer_info->dhtget);
+      peer_info->dhtget = NULL;
+    }
     return GNUNET_OK;
   }
 
@@ -5866,12 +5957,6 @@ handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
               "  not for us, retransmitting...\n");
   GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
   peer_info = peer_info_get (&msg->oid);
-  if (NULL == peer_info)
-  {
-    /* If we know the tunnel, we should DEFINITELY know the peer */
-    GNUNET_break (0);
-    return GNUNET_OK;
-  }
   send_prebuilt_message (message, &id, t);
   return GNUNET_OK;
 }
@@ -5883,8 +5968,6 @@ handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
- * @param atsi performance data
- * @param atsi_count number of records in 'atsi'
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  *
@@ -5892,9 +5975,7 @@ handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
  */
 static int
 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
-                       const struct GNUNET_MessageHeader *message,
-                       const struct GNUNET_ATS_Information *atsi,
-                       unsigned int atsi_count)
+                       const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_TunnelKeepAlive *msg;
   struct MeshTunnel *t;
@@ -5908,8 +5989,8 @@ handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
   if (NULL == t)
   {
     /* TODO notify that we dont know that tunnel */
-    GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
-    GNUNET_break_op (0);
+    GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
+                              GNUNET_NO);
     return GNUNET_OK;
   }
 
@@ -5939,6 +6020,8 @@ static struct GNUNET_CORE_MessageHandler core_handlers[] = {
   {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
   {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
     sizeof (struct GNUNET_MESH_ACK)},
+  {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
+    sizeof (struct GNUNET_MESH_Poll)},
   {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
    sizeof (struct GNUNET_MESH_PathACK)},
   {NULL, 0, 0}
@@ -5962,9 +6045,10 @@ static struct GNUNET_CORE_MessageHandler core_handlers[] = {
 static int
 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
-  struct GNUNET_CONTAINER_MultiHashMap *h = cls;
+  struct MeshClient *c = cls;
+
   GNUNET_break (GNUNET_YES ==
-                GNUNET_CONTAINER_multihashmap_remove (h, key, value));
+                GNUNET_CONTAINER_multihashmap_remove (applications, key, c));
   return GNUNET_OK;
 }
 
@@ -6080,10 +6164,10 @@ dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
   GNUNET_PEER_resolve (path_info->peer->id, &pi);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  for %s\n", GNUNET_i2s (&pi));
 
-  p = path_build_from_dht (get_path, get_path_length, put_path,
-                           put_path_length);
+  p = path_build_from_dht (get_path, get_path_length,
+                           put_path, put_path_length);
   path_add_to_peers (p, GNUNET_NO);
-  path_destroy(p);
+  path_destroy (p);
   for (i = 0; i < path_info->peer->ntunnels; i++)
   {
     tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
@@ -6129,162 +6213,28 @@ dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
   if (size != sizeof (struct PBlock))
   {
     GNUNET_break_op (0);
-    return;
-  }
-  if (ntohl(pb->type) != t->type)
-  {
-    GNUNET_break_op (0);
-    return;
-  }
-  GNUNET_assert (NULL != t->owner);
-  peer_info = peer_info_get (pi);
-  (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
-                                            peer_info,
-                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
-
-  p = path_build_from_dht (get_path, get_path_length, put_path,
-                           put_path_length);
-  path_add_to_peers (p, GNUNET_NO);
-  path_destroy(p);
-  tunnel_add_peer (t, peer_info);
-  peer_info_connect (peer_info, t);
-}
-
-
-/**
- * Function to process DHT string to regex matching.
- * Called on each result obtained for the DHT search.
- *
- * @param cls closure (search context)
- * @param exp when will this value expire
- * @param key key of the result
- * @param get_path path of the get request (not used)
- * @param get_path_length lenght of get_path (not used)
- * @param put_path path of the put request (not used)
- * @param put_path_length length of the put_path (not used)
- * @param type type of the result
- * @param size number of bytes in data
- * @param data pointer to the result data
- */
-static void
-dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
-                               const struct GNUNET_HashCode * key,
-                               const struct GNUNET_PeerIdentity *get_path,
-                               unsigned int get_path_length,
-                               const struct GNUNET_PeerIdentity *put_path,
-                               unsigned int put_path_length,
-                               enum GNUNET_BLOCK_Type type,
-                               size_t size, const void *data)
-{
-  const struct MeshRegexAccept *block = data;
-  struct MeshRegexSearchContext *ctx = cls;
-  struct MeshRegexSearchInfo *info = ctx->info;
-  struct MeshPeerPath *p;
-  struct MeshPeerInfo *peer_info;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  for %s\n", info->description);
-
-  peer_info = peer_info_get(&block->id);
-  p = path_build_from_dht (get_path, get_path_length, put_path,
-                           put_path_length);
-  path_add_to_peers (p, GNUNET_NO);
-  path_destroy(p);
-
-  tunnel_add_peer (info->t, peer_info);
-  peer_info_connect (peer_info, info->t);
-  if (0 == info->peer)
-  {
-    info->peer = peer_info->id;
-  }
-  else
-  {
-    GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
-  }
-
-  info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
-                                                &regex_connect_timeout,
-                                                info);
-
-  return;
-}
-
-
-/**
- * Function to process DHT string to regex matching.
- * Called on each result obtained for the DHT search.
- *
- * @param cls closure (search context)
- * @param exp when will this value expire
- * @param key key of the result
- * @param get_path path of the get request (not used)
- * @param get_path_length lenght of get_path (not used)
- * @param put_path path of the put request (not used)
- * @param put_path_length length of the put_path (not used)
- * @param type type of the result
- * @param size number of bytes in data
- * @param data pointer to the result data
- *
- * TODO: re-issue the request after certain time? cancel after X results?
- */
-static void
-dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
-                        const struct GNUNET_HashCode * key,
-                        const struct GNUNET_PeerIdentity *get_path,
-                        unsigned int get_path_length,
-                        const struct GNUNET_PeerIdentity *put_path,
-                        unsigned int put_path_length,
-                        enum GNUNET_BLOCK_Type type,
-                        size_t size, const void *data)
-{
-  const struct MeshRegexBlock *block = data;
-  struct MeshRegexSearchContext *ctx = cls;
-  struct MeshRegexSearchInfo *info = ctx->info;
-  void *copy;
-  size_t len;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "DHT GET STRING RETURNED RESULTS\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "  key: %s\n", GNUNET_h2s (key));
-
-  copy = GNUNET_malloc (size);
-  memcpy (copy, data, size);
-  GNUNET_break (GNUNET_OK ==
-                GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
-                                                  GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
-  len = ntohl (block->n_proof);
-  {
-    char proof[len + 1];
-
-    memcpy (proof, &block[1], len);
-    proof[len] = '\0';
-    if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
-    {
-      GNUNET_break_op (0);
-      return;
-    }
+    return;
   }
-  len = strlen (info->description);
-  if (len == ctx->position) // String processed
+  if (ntohl(pb->type) != t->type)
   {
-    if (GNUNET_YES == ntohl (block->accepting))
-    {
-      regex_find_path(key, ctx);
-    }
-    else
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  block not accepting!\n");
-      // FIXME REGEX this block not successful, wait for more? start timeout?
-    }
+    GNUNET_break_op (0);
     return;
   }
+  GNUNET_assert (NULL != t->owner);
+  peer_info = peer_info_get (pi);
+  (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
+                                            peer_info,
+                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
 
-  regex_next_edge (block, size, ctx);
-
-  return;
+  p = path_build_from_dht (get_path, get_path_length, put_path,
+                           put_path_length);
+  path_add_to_peers (p, GNUNET_NO);
+  path_destroy(p);
+  tunnel_add_peer (t, peer_info);
+  peer_info_connect (peer_info, t);
 }
 
+
 /******************************************************************************/
 /*********************       MESH LOCAL HANDLES      **************************/
 /******************************************************************************/
@@ -6310,6 +6260,7 @@ handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (SERVER DOWN)\n");
     return;
   }
+
   c = clients;
   while (NULL != c)
   {
@@ -6338,7 +6289,7 @@ handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
     /* deregister clients applications */
     if (NULL != c->apps)
     {
-      GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
+      GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c);
       GNUNET_CONTAINER_multihashmap_destroy (c->apps);
     }
     if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
@@ -6351,7 +6302,9 @@ handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
       GNUNET_CONTAINER_multihashmap_destroy (c->types);
     for (i = 0; i < c->n_regex; i++)
     {
-      GNUNET_free (c->regexes[i]);
+      GNUNET_free (c->regexes[i].regex);
+      if (NULL != c->regexes[i].h)
+       GNUNET_REGEX_announce_cancel (c->regexes[i].h);
     }
     GNUNET_free_non_null (c->regexes);
     if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
@@ -6414,7 +6367,7 @@ handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_MESH_ApplicationType at;
     struct GNUNET_HashCode hc;
 
-    c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
+    c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
     for (i = 0; i < napps; i++)
     {
       at = ntohl (a[i]);
@@ -6438,7 +6391,7 @@ handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
     struct GNUNET_HashCode hc;
 
     t = (uint16_t *) & a[napps];
-    c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
+    c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
     for (i = 0; i < ntypes; i++)
     {
       u16 = ntohs (t[i]);
@@ -6457,9 +6410,9 @@ handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
               " client has %u+%u subscriptions\n", napps, ntypes);
 
   GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
-  c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
-  c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
-  c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
+  c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
+  c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
+  c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
   GNUNET_SERVER_notification_context_add (nc, client);
   GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
 
@@ -6479,9 +6432,12 @@ static void
 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
                              const struct GNUNET_MessageHeader *message)
 {
+  const struct GNUNET_MESH_RegexAnnounce *msg;
+  struct MeshRegexDescriptor rd;
   struct MeshClient *c;
   char *regex;
   size_t len;
+  size_t offset;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
 
@@ -6494,18 +6450,50 @@ handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
 
-  len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
-  regex = GNUNET_malloc (len + 1);
-  memcpy (regex, &message[1], len);
-  regex[len] = '\0';
-  GNUNET_array_append (c->regexes, c->n_regex, regex);
+  msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
+
+  len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
+  if (NULL != c->partial_regex)
+  {
+    regex = c->partial_regex;
+    offset = strlen (c->partial_regex);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "  continuation, already have %u bytes\n",
+                offset);
+  }
+  else
+  {
+    regex = NULL;
+    offset = 0;
+  }
+
+  regex = GNUNET_realloc (regex, offset + len + 1);
+  memcpy (&regex[offset], &msg[1], len);
+  regex[offset + len] = '\0';
+  if (0 == ntohs (msg->last))
+  {
+    c->partial_regex = regex;
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "  not ended, stored %u bytes for later\n",
+                len);
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    return;
+  }
+  rd.regex = regex;
+  rd.compression = ntohs (msg->compression_characters);
+  rd.h = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  length %u\n", len);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  regex %s\n", regex);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  compr %u\n", ntohs (rd.compression));
+  GNUNET_array_append (c->regexes, c->n_regex, rd);
+  c->partial_regex = NULL;
   if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
   {
-    c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
+    c->regex_announce_task = GNUNET_SCHEDULER_add_now (&regex_announce, c);
   }
   else
   {
-    regex_put(regex);
+    regex_put (&rd);
   }
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
@@ -6577,7 +6565,7 @@ handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
   next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
               GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
-  t->peers = GNUNET_CONTAINER_multihashmap_create (32);
+  t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -6636,30 +6624,18 @@ handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
   if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
   {
     client_ignore_tunnel (c, t);
-#if 0
-    // TODO: when to destroy incoming tunnel?
-    if (t->nclients == 0)
-    {
-      GNUNET_assert (GNUNET_YES ==
-                     GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
-                                                           &hash, t));
-      GNUNET_assert (GNUNET_YES ==
-                     GNUNET_CONTAINER_multihashmap_remove (t->peers,
-                                                           &my_full_id.hashPubKey,
-                                                           t));
-    }
-#endif
+    tunnel_destroy_empty (t);
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
-  send_client_tunnel_disconnect(t, c);
-  client_delete_tunnel(c, t);
+  send_client_tunnel_disconnect (t, c);
+  client_delete_tunnel (c, t);
 
   /* Don't try to ACK the client about the tunnel_destroy multicast packet */
   t->owner = NULL;
-  tunnel_send_destroy (t);
+  tunnel_send_destroy (t, 0);
   t->destroy = GNUNET_YES;
-  // The tunnel will be destroyed when the last message is transmitted.
+  /* The tunnel will be destroyed when the last message is transmitted. */
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
   return;
 }
@@ -7170,16 +7146,14 @@ handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
                                 const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_ConnectPeerByString *msg;
-  struct MeshRegexSearchContext *ctx;
   struct MeshRegexSearchInfo *info;
-  struct GNUNET_DHT_GetHandle *get_h;
-  struct GNUNET_HashCode key;
   struct MeshTunnel *t;
   struct MeshClient *c;
   MESH_TunnelNumber tid;
   const char *string;
   size_t size;
   size_t len;
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Connect by string started\n");
   msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
@@ -7227,7 +7201,7 @@ handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
 
   /* Only one connect_by_string allowed at the same time! */
   /* FIXME: allow more, return handle at api level to cancel, document */
-  if (NULL != t->regex_ctx)
+  if (NULL != t->regex_search)
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -7238,44 +7212,17 @@ handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
   len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
   string = (const char *) &msg[1];
 
-  /* Initialize context */
-  size = GNUNET_REGEX_get_first_key(string, len, &key);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "  consumed %u bits out of %u\n", size, len);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "  looking for %s\n", GNUNET_h2s (&key));
-
   info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
   info->t = t;
-  info->description = GNUNET_malloc (len + 1);
-  memcpy (info->description, string, len);
-  info->description[len] = '\0';
-  info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
-  info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
+  info->description = GNUNET_strndup (string, len);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   string: %s\n", info->description);
 
-  ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
-  ctx->position = size;
-  ctx->info = info;
-  t->regex_ctx = ctx;
-
-  GNUNET_array_append (info->contexts, info->n_contexts, ctx);
-
-  /* Start search in DHT */
-  get_h = GNUNET_DHT_get_start (dht_handle,    /* handle */
-                                GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
-                                &key,     /* key to search */
-                                dht_replication_level, /* replication level */
-                                GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
-                                NULL,       /* xquery */ // FIXME BLOOMFILTER
-                                0,     /* xquery bits */ // FIXME BLOOMFILTER SIZE
-                                &dht_get_string_handler, ctx);
-
-  GNUNET_break (GNUNET_OK ==
-                GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
-                                                  &key,
-                                                  get_h,
-                                                  GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
+  t->regex_search = info;
+
+  info->search_handle = GNUNET_REGEX_search (dht_handle,
+                                             info->description,
+                                             &regex_found_handler, info,
+                                             stats);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
@@ -7378,7 +7325,7 @@ handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
     copy->ttl = htonl (default_ttl);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "  calling generic handler...\n");
-    handle_mesh_data_unicast (NULL, &my_full_id, &copy->header, NULL, 0);
+    handle_mesh_data_unicast (NULL, &my_full_id, &copy->header);
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -7467,11 +7414,11 @@ handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
+  clinfo->bck_pid++;
 
   /* Ok, everything is correct, send the message
    * (pretend we got it from a mesh peer)
    */
-  clinfo->bck_pid++;
   {
     char buf[ntohs (message->size)] GNUNET_ALIGN;
     struct GNUNET_MESH_ToOrigin *copy;
@@ -7482,20 +7429,12 @@ handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_PEER_resolve (t->id.oid, &copy->oid);
     copy->tid = htonl (t->id.tid);
     copy->ttl = htonl (default_ttl);
-    if (ntohl (copy->pid) != (t->bck_pid + 1))
-    {
-      GNUNET_break (0);
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  "To Origin PID, expected %u, got %u\n",
-                  t->bck_pid + 1,
-                  ntohl (copy->pid));
-      return;
-    }
-    t->bck_pid++;
+    copy->pid = htonl (t->bck_pid + 1);
+
     copy->sender = my_full_id;
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "  calling generic handler...\n");
-    handle_mesh_data_to_orig (NULL, &my_full_id, &copy->header, NULL, 0);
+    handle_mesh_data_to_orig (NULL, &my_full_id, &copy->header);
   }
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 
@@ -7586,7 +7525,7 @@ handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "  calling generic handler...\n");
-    handle_mesh_data_multicast (client, &my_full_id, &copy->header, NULL, 0);
+    handle_mesh_data_multicast (client, &my_full_id, &copy->header);
   }
 
   GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
@@ -7639,7 +7578,7 @@ handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
   ack = ntohl (msg->max_pid);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ack %u\n", ack);
 
-  /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
+  /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
   if (NULL != t->owner && t->owner->handle == client)
   {
     /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
@@ -7653,12 +7592,298 @@ handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
   }
 
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);  
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 
   return;
 }
 
 
+/**
+ * Iterator over all peers to send a monitoring client info about a tunnel.
+ *
+ * @param cls Closure (message being built).
+ * @param key Key (hashed tunnel ID, unused).
+ * @param value Peer info.
+ *
+ * @return GNUNET_YES, to keep iterating.
+ */
+static int
+monitor_peers_iterator (void *cls,
+                        const struct GNUNET_HashCode * key,
+                        void *value)
+{
+  struct GNUNET_MESH_LocalMonitor *msg = cls;
+  struct GNUNET_PeerIdentity *id;
+  struct MeshPeerInfo *info = value;
+
+  id = (struct GNUNET_PeerIdentity *) &msg[1];
+  GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
+  msg->npeers++;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "*    sending info about peer %s [%u]\n",
+              GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
+
+  return GNUNET_YES;
+}
+
+
+
+/**
+ * Iterator over all tunnels to send a monitoring client info about each tunnel.
+ *
+ * @param cls Closure (client handle).
+ * @param key Key (hashed tunnel ID, unused).
+ * @param value Tunnel info.
+ *
+ * @return GNUNET_YES, to keep iterating.
+ */
+static int
+monitor_all_tunnels_iterator (void *cls,
+                              const struct GNUNET_HashCode * key,
+                              void *value)
+{
+  struct GNUNET_SERVER_Client *client = cls;
+  struct MeshTunnel *t = value;
+  struct GNUNET_MESH_LocalMonitor *msg;
+  uint32_t npeers;
+  
+  npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
+  msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
+  npeers * sizeof (struct GNUNET_PeerIdentity));
+  GNUNET_PEER_resolve(t->id.oid, &msg->owner);
+  msg->tunnel_id = htonl (t->id.tid);
+  msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
+  npeers * sizeof (struct GNUNET_PeerIdentity));
+  msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
+  msg->npeers = 0;
+  (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
+                                                monitor_peers_iterator,
+                                                msg);
+  
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "*  sending info about tunnel %s [%u] (%u peers)\n",
+              GNUNET_i2s (&msg->owner), t->id.tid, npeers);
+  
+  if (msg->npeers != npeers)
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Get tunnels fail: size %u - iter %u\n",
+                npeers, msg->npeers);
+  }
+  
+    msg->npeers = htonl (npeers);
+    GNUNET_SERVER_notification_context_unicast (nc, client,
+                                                &msg->header, GNUNET_NO);
+    return GNUNET_YES;
+}
+
+
+/**
+ * Handler for client's MONITOR request.
+ *
+ * @param cls Closure (unused).
+ * @param client Identification of the client.
+ * @param message The actual message.
+ */
+static void
+handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
+                          const struct GNUNET_MessageHeader *message)
+{
+  struct MeshClient *c;
+
+  /* Sanity check for client registration */
+  if (NULL == (c = client_get (client)))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Received get tunnels request from client %u\n",
+              c->id);
+  GNUNET_CONTAINER_multihashmap_iterate (tunnels,
+                                         monitor_all_tunnels_iterator,
+                                         client);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Get tunnels request from client %u completed\n",
+              c->id);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
+/**
+ * Data needed to build a Monitor_Tunnel message.
+ */
+struct MeshMonitorTunnelContext
+{
+  /**
+   * Partial message, including peer count.
+   */
+  struct GNUNET_MESH_LocalMonitor *msg;
+
+  /**
+   * Hashmap with positions: peer->position.
+   */
+  struct GNUNET_CONTAINER_MultiHashMap *lookup;
+
+  /**
+   * Index of the parent of each peer in the message, realtive to the absolute
+   * order in the array (can be in a previous message).
+   */
+  uint32_t parents[1024];
+
+  /**
+   * Peers visited so far in the tree, aka position of the current peer.
+   */
+  unsigned int npeers;
+
+  /**
+   * Client requesting the info.
+   */
+  struct MeshClient *c;
+};
+
+
+/**
+ * Send a client a message about the structure of a tunnel.
+ *
+ * @param ctx Context of the tunnel iteration, with info regarding the state
+ *            of the execution and the number of peers visited for this message.
+ */
+static void
+send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
+{
+  struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
+  struct GNUNET_PeerIdentity *pid;
+  unsigned int *parent;
+  size_t size;
+
+  size = sizeof (struct GNUNET_MESH_LocalMonitor);
+  size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
+  resp->header.size = htons (size);
+  pid = (struct GNUNET_PeerIdentity *) &resp[1];
+  parent = (unsigned int *) &pid[resp->npeers];
+  memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
+  GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
+                                              &resp->header, GNUNET_NO);
+}
+
+/**
+ * Iterator over a tunnel tree to build a message containing all peers
+ * the in the tunnel, including relay nodes.
+ *
+ * @param cls Closure (pointer to pointer of message being built).
+ * @param peer Short ID of a peer.
+ * @param parent Short ID of the @c peer 's parent.
+ */
+static void
+tunnel_tree_iterator (void *cls,
+                      GNUNET_PEER_Id peer,
+                      GNUNET_PEER_Id parent)
+{
+  struct MeshMonitorTunnelContext *ctx = cls;
+  struct GNUNET_MESH_LocalMonitor *msg;
+  struct GNUNET_PeerIdentity *pid;
+  struct GNUNET_PeerIdentity ppid;
+
+  msg = ctx->msg;
+  pid = (struct GNUNET_PeerIdentity *) &msg[1];
+  GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
+  GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
+                                     &pid[msg->npeers].hashPubKey,
+                                     (void *) (long) ctx->npeers,
+                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+  GNUNET_PEER_resolve (parent, &ppid);
+  ctx->parents[msg->npeers] =
+      htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
+                                                       &ppid.hashPubKey));
+
+  ctx->npeers++;
+  msg->npeers++;
+
+  if (sizeof (struct GNUNET_MESH_LocalMonitor) +
+      (msg->npeers + 1) *
+      (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
+      > USHRT_MAX)
+  {
+    send_client_tunnel_info (ctx);
+    msg->npeers = 0;
+  }
+}
+
+
+/**
+ * Handler for client's MONITOR_TUNNEL request.
+ *
+ * @param cls Closure (unused).
+ * @param client Identification of the client.
+ * @param message The actual message.
+ */
+static void
+handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
+                          const struct GNUNET_MessageHeader *message)
+{
+  const struct GNUNET_MESH_LocalMonitor *msg;
+  struct GNUNET_MESH_LocalMonitor *resp;
+  struct MeshMonitorTunnelContext ctx;
+  struct MeshClient *c;
+  struct MeshTunnel *t;
+
+  /* Sanity check for client registration */
+  if (NULL == (c = client_get (client)))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  msg = (struct GNUNET_MESH_LocalMonitor *) message;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Received tunnel info request from client %u for tunnel %s[%X]\n",
+              c->id,
+              &msg->owner,
+              ntohl (msg->tunnel_id));
+  t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
+  if (NULL == t)
+  {
+    /* We don't know the tunnel */
+    struct GNUNET_MESH_LocalMonitor warn;
+
+    warn = *msg;
+    warn.npeers = htonl (UINT_MAX);
+    GNUNET_SERVER_notification_context_unicast (nc, client,
+                                                &warn.header,
+                                                GNUNET_NO);
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    return;
+  }
+
+  /* Initialize context */
+  resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
+  *resp = *msg;
+  resp->npeers = 0;
+  ctx.msg = resp;
+  ctx.lookup = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
+  ctx.c = c;
+
+  /* Collect and send information */
+  tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
+  send_client_tunnel_info (&ctx);
+
+  /* Free context */
+  GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
+  GNUNET_free (resp);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Monitor tunnel request from client %u completed\n",
+              c->id);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
 /**
  * Functions to handle messages from clients
  */
@@ -7711,6 +7936,12 @@ static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
   {&handle_local_ack, NULL,
    GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
    sizeof (struct GNUNET_MESH_LocalAck)},
+  {&handle_local_get_tunnels, NULL,
+   GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
+   sizeof (struct GNUNET_MessageHeader)},
+  {&handle_local_show_tunnel, NULL,
+   GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
+     sizeof (struct GNUNET_MESH_LocalMonitor)},
   {NULL, NULL, 0, 0}
 };
 
@@ -7733,6 +7964,12 @@ core_init (void *cls, struct GNUNET_CORE_Handle *server,
       NULL == server)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                " core id %s\n",
+                GNUNET_i2s (identity));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                " my id %s\n",
+                GNUNET_i2s (&my_full_id));
     GNUNET_SCHEDULER_shutdown (); // Try gracefully
     if (10 < i++)
       GNUNET_abort(); // Try harder
@@ -7746,13 +7983,9 @@ core_init (void *cls, struct GNUNET_CORE_Handle *server,
  *
  * @param cls closure
  * @param peer peer identity this notification is about
- * @param atsi performance data for the connection
- * @param atsi_count number of records in 'atsi'
  */
 static void
-core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
-              const struct GNUNET_ATS_Information *atsi,
-              unsigned int atsi_count)
+core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
   struct MeshPeerInfo *peer_info;
   struct MeshPeerPath *path;
@@ -7892,9 +8125,9 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_CORE_disconnect (core_handle);
     core_handle = NULL;
   }
- if (NULL != keygen)
 if (NULL != keygen)
   {
-    GNUNET_CRYPTO_rsa_key_create_stop (keygen);
+    GNUNET_CRYPTO_ecc_key_create_stop (keygen);
     keygen = NULL;
   }
   GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
@@ -7914,22 +8147,28 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_SCHEDULER_cancel (announce_id_task);
     announce_id_task = GNUNET_SCHEDULER_NO_TASK;
   }
+  if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
+  {
+    GNUNET_SCHEDULER_cancel (announce_applications_task);
+    announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
+  }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
 }
 
 
 /**
- * Callback for hostkey read/generation
+ * Callback for hostkey read/generation.
  *
- * @param cls NULL
- * @param pk the private key
- * @param emsg error message
+ * @param cls Closure (Configuration handle).
+ * @param pk The ECC private key.
+ * @param emsg Error message, if any.
  */
 static void
 key_generation_cb (void *cls,
-                   struct GNUNET_CRYPTO_RsaPrivateKey *pk,
+                   struct GNUNET_CRYPTO_EccPrivateKey *pk,
                    const char *emsg)
 {
+  const struct GNUNET_CONFIGURATION_Handle *c = cls;
   struct MeshPeerInfo *peer;
   struct MeshPeerPath *p;
 
@@ -7937,12 +8176,13 @@ key_generation_cb (void *cls,
   if (NULL == pk)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Mesh service could not access hostkey.  Exiting.\n"));
+                _("Mesh service could not access hostkey: %s. Exiting.\n"),
+                emsg);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
   my_private_key = pk;
-  GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
+  GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
                       &my_full_id.hashPubKey);
   myid = GNUNET_PEER_intern (&my_full_id);
@@ -7950,14 +8190,23 @@ key_generation_cb (void *cls,
               "Mesh for peer [%s] starting\n",
               GNUNET_i2s(&my_full_id));
 
-//   transport_handle = GNUNET_TRANSPORT_connect(c,
-//                                               &my_full_id,
-//                                               NULL,
-//                                               NULL,
-//                                               NULL,
-//                                               NULL);
-
-
+  core_handle = GNUNET_CORE_connect (c, /* Main configuration */
+                                     NULL,      /* Closure passed to MESH functions */
+                                     &core_init,        /* Call core_init once connected */
+                                     &core_connect,     /* Handle connects */
+                                     &core_disconnect,  /* remove peers on disconnects */
+                                     NULL,      /* Don't notify about all incoming messages */
+                                     GNUNET_NO, /* For header only in notification */
+                                     NULL,      /* Don't notify about all outbound messages */
+                                     GNUNET_NO, /* For header-only out notification */
+                                     core_handlers);    /* Register these handlers */
+  
+  if (core_handle == NULL)
+  {
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
 
   next_tid = 0;
   next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
@@ -8002,32 +8251,15 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
   server_handle = server;
-  core_handle = GNUNET_CORE_connect (c, /* Main configuration */
-                                     NULL,      /* Closure passed to MESH functions */
-                                     &core_init,        /* Call core_init once connected */
-                                     &core_connect,     /* Handle connects */
-                                     &core_disconnect,  /* remove peers on disconnects */
-                                     NULL,      /* Don't notify about all incoming messages */
-                                     GNUNET_NO, /* For header only in notification */
-                                     NULL,      /* Don't notify about all outbound messages */
-                                     GNUNET_NO, /* For header-only out notification */
-                                     core_handlers);    /* Register these handlers */
-
-  if (core_handle == NULL)
-  {
-    GNUNET_break (0);
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
 
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
+      GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
                                                &keyfile))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _
                 ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "mesh", "hostkey");
+                "mesh", "peer/privatekey");
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -8055,7 +8287,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "APP_ANNOUNCE_TIME %llu ms\n", 
+             app_announce_time.rel_value);
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
                                            &id_announce_time))
@@ -8067,21 +8301,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  else
-  {
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
-                                           &unacknowledged_wait_time))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  Exiting.\n"),
-                "mesh", "unacknowledged wait time");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
 
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
@@ -8130,6 +8349,16 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     default_ttl = 64;
   }
 
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
+                                             &max_peers))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
+                "mesh", "max peers", 1000);
+    max_peers = 1000;
+  }
+
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
                                              &dht_replication_level))
@@ -8137,15 +8366,15 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 _
                 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
-                "mesh", "dht replication level", 10);
-    dht_replication_level = 10;
+                "mesh", "dht replication level", 3);
+    dht_replication_level = 3;
   }
 
-  tunnels = GNUNET_CONTAINER_multihashmap_create (32);
-  incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
-  peers = GNUNET_CONTAINER_multihashmap_create (32);
-  applications = GNUNET_CONTAINER_multihashmap_create (32);
-  types = GNUNET_CONTAINER_multihashmap_create (32);
+  tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
+  incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
+  peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
+  applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
+  types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
 
   dht_handle = GNUNET_DHT_connect (c, 64);
   if (NULL == dht_handle)
@@ -8158,7 +8387,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   /* Scheduled the task to clean up when shutdown is called */
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
                                 NULL);
-  keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
+  keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
+                                               &key_generation_cb,
+                                               (void *) c);
   GNUNET_free (keyfile);
 }