optimize mqm_head scans by avoiding constantly scanning over definitively non-ready...
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_peer.c
index ee8f9652f5e899b847a026ee541bf919020b6acb..fa3f2be806f590a23e856a834039562adaaf832a 100644 (file)
 /******************************************************************************/
 
 /**
- * Struct containing info about a queued transmission to this peer
- */
-struct CadetPeerQueue
-{
-  /**
-   * DLL next
-   */
-  struct CadetPeerQueue *next;
-
-  /**
-   * DLL previous
-   */
-  struct CadetPeerQueue *prev;
-
-  /**
-   * Peer this transmission is directed to.
-   */
-  struct CadetPeer *peer;
-
-  /**
-   * Connection this message belongs to.
-   */
-  struct CadetConnection *c;
-
-  /**
-   * Is FWD in c?
-   */
-  int fwd;
-
-  /**
-   * Pointer to info stucture used as cls.
-   */
-  void *cls;
-
-  /**
-   * Type of message
-   */
-  uint16_t type;
-
-  /**
-   * Type of message
-   */
-  uint16_t payload_type;
-
-  /**
-   * Type of message
-   */
-  uint32_t payload_id;
-
-  /**
-   * Size of the message
-   */
-  size_t size;
-
-  /**
-   * Set when this message starts waiting for CORE.
-   */
-  struct GNUNET_TIME_Absolute start_waiting;
-
-  /**
-   * Function to call on sending.
-   */
-  GCP_sent cont;
-
-  /**
-   * Closure for callback.
-   */
-  void *cont_cls;
+ * Information about a queued message on the peer level.
+ */
+struct CadetPeerQueue {
+
+    struct CadetPeerQueue *next;
+    struct CadetPeerQueue *prev;
+
+    /**
+     * Envelope to cancel message before MQ sends it.
+     */
+    struct GNUNET_MQ_Envelope *env;
+
+    /**
+     * Peer (neighbor) this message is being sent to.
+     */
+    struct CadetPeer *peer;
+
+    /**
+     * Continuation to call to notify higher layers about message sent.
+     */
+    GCP_sent cont;
+
+    /**
+     * Closure for @a cont.
+     */
+    void *cont_cls;
+
+    /**
+     * Task to asynchronously run the drop continuation.
+     */
+    struct GNUNET_SCHEDULER_Task *drop_task;
+
+    /**
+     * Time when message was queued for sending.
+     */
+    struct GNUNET_TIME_Absolute queue_timestamp;
+
+    /**
+     * #GNUNET_YES if message was management traffic (POLL, ACK, ...).
+     */
+    int management_traffic;
+
+    /**
+     * Message type.
+     */
+    uint16_t type;
+
+    /**
+     * Message size.
+     */
+    uint16_t size;
+
+    /**
+     * Type of the message's payload, if it was encrypted data.
+     */
+    uint16_t payload_type;
+
+    /**
+     * ID of the payload (PID, ACK #, ...).
+     */
+    struct CadetEncryptedMessageIdentifier payload_id;
+
+    /**
+     * Connection this message was sent on.
+     */
+    struct CadetConnection *c;
+
+    /**
+     * Direction in @a c this message was send on (#GNUNET_YES = FWD).
+     */
+    int c_fwd;
 };
 
 
@@ -121,86 +124,74 @@ struct CadetPeerQueue
  */
 struct CadetPeer
 {
-  /**
-   * ID of the peer
-   */
-  GNUNET_PEER_Id id;
-
-  /**
-   * Last time we heard from this peer
-   */
-  struct GNUNET_TIME_Absolute last_contact;
-
-  /**
-   * Paths to reach the peer, ordered by ascending hop count
-   */
-  struct CadetPeerPath *path_head;
-
-  /**
-   * Paths to reach the peer, ordered by ascending hop count
-   */
-  struct CadetPeerPath *path_tail;
-
-  /**
-   * Handle to stop the DHT search for paths to this peer
-   */
-  struct GCD_search_handle *search_h;
-
-  /**
-   * Handle to stop the DHT search for paths to this peer
-   */
-  struct GNUNET_SCHEDULER_Task *search_delayed;
-
-  /**
-   * Tunnel to this peer, if any.
-   */
-  struct CadetTunnel *tunnel;
-
-  /**
-   * Connections that go through this peer; indexed by tid.
-   */
-  struct GNUNET_CONTAINER_MultiHashMap *connections;
-
-  /**
-   * Handle for queued transmissions
-   */
-  struct GNUNET_CORE_TransmitHandle *core_transmit;
-
-  /**
-   * Timestamp
-   */
-  struct GNUNET_TIME_Absolute tmt_time;
-
-  /**
-   * Transmission queue to core DLL head
-   */
-  struct CadetPeerQueue *queue_head;
-
-  /**
-   * Transmission queue to core DLL tail
-   */
-  struct CadetPeerQueue *queue_tail;
-
-  /**
-   * How many messages are in the queue to this peer.
-   */
-  unsigned int queue_n;
-
-  /**
-   * Hello message.
-   */
-  struct GNUNET_HELLO_Message* hello;
-
-  /**
-   * Handle to us offering the HELLO to the transport.
-   */
-  struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
-
-  /**
-   * Handle to our ATS request asking ATS to suggest an address
-   * to TRANSPORT for this peer (to establish a direct link).
-   */
-  struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
+    /**
+     * ID of the peer
+     */
+    GNUNET_PEER_Id id;
+
+    struct CadetPeerQueue *q_head;
+    struct CadetPeerQueue *q_tail;
+
+    /**
+     * Last time we heard from this peer
+     */
+    struct GNUNET_TIME_Absolute last_contact;
+
+    /**
+     * Paths to reach the peer, ordered by ascending hop count
+     */
+    struct CadetPeerPath *path_head;
+
+    /**
+     * Paths to reach the peer, ordered by ascending hop count
+     */
+    struct CadetPeerPath *path_tail;
+
+    /**
+     * Handle to stop the DHT search for paths to this peer
+     */
+    struct GCD_search_handle *search_h;
+
+    /**
+     * Handle to stop the DHT search for paths to this peer
+     */
+    struct GNUNET_SCHEDULER_Task *search_delayed;
+
+    /**
+     * Tunnel to this peer, if any.
+     */
+    struct CadetTunnel *tunnel;
+
+    /**
+     * Connections that go through this peer; indexed by tid.
+     */
+    struct GNUNET_CONTAINER_MultiShortmap  *connections;
+
+    /**
+     * Handle for core transmissions.
+     */
+    struct GNUNET_MQ_Handle *core_mq;
+
+    /**
+     * How many messages are in the queue to this peer.
+     */
+    unsigned int queue_n;
+
+    /**
+     * Hello message.
+     */
+    struct GNUNET_HELLO_Message* hello;
+
+    /**
+     * Handle to us offering the HELLO to the transport.
+     */
+    struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
+
+    /**
+     * Handle to our ATS request asking ATS to suggest an address
+     * to TRANSPORT for this peer (to establish a direct link).
+     */
+    struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
 
 };
 
@@ -245,14 +236,14 @@ static unsigned long long drop_percent;
 static struct GNUNET_CORE_Handle *core_handle;
 
 /**
- * Handle to communicate with ATS.
+ * Our configuration;
  */
-static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
- * Handle to try to start new connections.
+ * Handle to communicate with ATS.
  */
-static struct GNUNET_TRANSPORT_Handle *transport_handle;
+static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
 
 /**
  * Shutdown falg.
@@ -260,98 +251,6 @@ static struct GNUNET_TRANSPORT_Handle *transport_handle;
 static int in_shutdown;
 
 
-/******************************************************************************/
-/*****************************     DEBUG      *********************************/
-/******************************************************************************/
-
-/**
- * Log all kinds of info about the queueing status of a peer.
- *
- * @param p Peer whose queue to show.
- * @param level Error level to use for logging.
- */
-static void
-queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
-{
-  struct GNUNET_TIME_Relative core_wait_time;
-  struct CadetPeerQueue *q;
-  int do_log;
-
-  do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
-                                       "cadet-p2p",
-                                       __FILE__, __FUNCTION__, __LINE__);
-  if (0 == do_log)
-    return;
-
-  LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
-  LOG2 (level, "QQQ  queue length: %u\n", p->queue_n);
-  LOG2 (level, "QQQ  core tmt rdy: %p\n", p->core_transmit);
-  if (NULL != p->core_transmit)
-  {
-    core_wait_time = GNUNET_TIME_absolute_get_duration (p->tmt_time);
-    LOG2 (level, "QQQ  core called %s ago\n",
-          GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
-  }
-  for (q = p->queue_head; NULL != q; q = q->next)
-  {
-    LOG2 (level, "QQQ  - %s %s on %s\n",
-         GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
-    LOG2 (level, "QQQ    payload %s, %u\n",
-         GC_m2s (q->payload_type), q->payload_id);
-    LOG2 (level, "QQQ    size: %u bytes\n", q->size);
-  }
-
-  LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
-}
-
-
-/**
- * Log all kinds of info about a peer.
- *
- * @param peer Peer.
- */
-void
-GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
-{
-  struct CadetPeerPath *path;
-  unsigned int conns;
-  int do_log;
-
-  do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
-                                       "cadet-p2p",
-                                       __FILE__, __FUNCTION__, __LINE__);
-  if (0 == do_log)
-    return;
-
-  if (NULL == p)
-  {
-    LOG2 (level, "PPP DEBUG PEER NULL\n");
-    return;
-  }
-
-  LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
-  LOG2 (level, "PPP last contact %s\n",
-       GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
-  for (path = p->path_head; NULL != path; path = path->next)
-  {
-    char *s;
-
-    s = path_2s (path);
-    LOG2 (level, "PPP path: %s\n", s);
-    GNUNET_free (s);
-  }
-
-  LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
-  LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
-  conns = 0;
-  if (NULL != p->connections)
-    conns += GNUNET_CONTAINER_multihashmap_size (p->connections);
-  LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
-  queue_debug (p, level);
-  LOG2 (level, "PPP DEBUG END\n");
-}
-
-
 /******************************************************************************/
 /*****************************  CORE HELPERS  *********************************/
 /******************************************************************************/
@@ -369,17 +268,17 @@ GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
  */
 static int
 notify_broken (void *cls,
-               const struct GNUNET_HashCode *key,
+               const struct GNUNET_ShortHashCode *key,
                void *value)
 {
-  struct CadetPeer *peer = cls;
-  struct CadetConnection *c = value;
+    struct CadetPeer *peer = cls;
+    struct CadetConnection *c = value;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Notifying %s due to %s disconnect\n",
-       GCC_2s (c), GCP_2s (peer));
-  GCC_neighbor_disconnected (c, peer);
-  return GNUNET_YES;
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Notifying %s due to %s disconnect\n",
+         GCC_2s (c), GCP_2s (peer));
+    GCC_neighbor_disconnected (c, peer);
+    return GNUNET_YES;
 }
 
 
@@ -391,21 +290,32 @@ notify_broken (void *cls,
 static struct CadetPeerPath *
 pop_direct_path (struct CadetPeer *peer)
 {
-  struct CadetPeerPath *iter;
+    struct CadetPeerPath *iter;
 
-  for (iter = peer->path_head; NULL != iter; iter = iter->next)
-  {
-    if (2 >= iter->length)
+    for (iter = peer->path_head; NULL != iter; iter = iter->next)
     {
-      GNUNET_CONTAINER_DLL_remove (peer->path_head,
-                                  peer->path_tail,
-                                  iter);
-      return iter;
+        if (2 >= iter->length)
+        {
+            GNUNET_CONTAINER_DLL_remove (peer->path_head,
+                                         peer->path_tail,
+                                         iter);
+            return iter;
+        }
     }
-  }
-  return NULL;
+    return NULL;
 }
 
+/**
+ * Call the continuation after a message has been sent or dropped.
+ *
+ * This funcion removes the message from the queue.
+ *
+ * @param q Queue handle.
+ * @param sent #GNUNET_YES if was sent to CORE, #GNUNET_NO if dropped.
+ */
+static void
+call_peer_cont (struct CadetPeerQueue *q, int sent);
+
 
 /******************************************************************************/
 /***************************** CORE CALLBACKS *********************************/
@@ -415,274 +325,373 @@ pop_direct_path (struct CadetPeer *peer)
 /**
  * Method called whenever a given peer connects.
  *
- * @param cls closure
- * @param peer peer identity this notification is about
+ * @param cls Core closure (unused).
+ * @param peer Peer identity this notification is about
+ * @param mq Message Queue to this peer.
+ *
+ * @return Internal closure for handlers (CadetPeer struct).
  */
-static void
-core_connect (void *cls,
-              const struct GNUNET_PeerIdentity *peer)
-{
-  struct CadetPeer *neighbor;
-  struct CadetPeerPath *path;
-  char own_id[16];
-
-  GCC_check_connections ();
-  GNUNET_snprintf (own_id,
-                   sizeof (own_id),
-                   "%s",
-                   GNUNET_i2s (&my_full_id));
-  neighbor = GCP_get (peer, GNUNET_YES);
-  if (myid == neighbor->id)
-  {
-    LOG (GNUNET_ERROR_TYPE_INFO,
-         "CONNECTED %s (self)\n",
-         own_id);
-    path = path_new (1);
-  }
-  else
-  {
-    LOG (GNUNET_ERROR_TYPE_INFO,
-         "CONNECTED %s <= %s\n",
-         own_id,
-         GNUNET_i2s (peer));
-    path = path_new (2);
-    path->peers[1] = neighbor->id;
-    GNUNET_PEER_change_rc (neighbor->id, 1);
-  }
-  path->peers[0] = myid;
-  GNUNET_PEER_change_rc (myid, 1);
-  GCP_add_path (neighbor, path, GNUNET_YES);
+static void *
+core_connect_handler (void *cls,
+                      const struct GNUNET_PeerIdentity *peer,
+                      struct GNUNET_MQ_Handle *mq)
+{
+    struct CadetPeer *neighbor;
+    struct CadetPeerPath *path;
+    char own_id[16];
 
-  GNUNET_assert (NULL == neighbor->connections);
-  neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
-  GNUNET_assert (NULL != neighbor->connections);
+    GCC_check_connections ();
+    GNUNET_snprintf (own_id,
+                     sizeof (own_id),
+                     "%s",
+                     GNUNET_i2s (&my_full_id));
+
+    /* Save a path to the neighbor */
+    neighbor = GCP_get (peer, GNUNET_YES);
+    if (myid == neighbor->id)
+    {
+        LOG (GNUNET_ERROR_TYPE_INFO,
+             "CONNECTED %s (self)\n",
+             own_id);
+        path = path_new (1);
+    }
+    else
+    {
+        LOG (GNUNET_ERROR_TYPE_INFO,
+             "CONNECTED %s <= %s\n",
+             own_id,
+             GNUNET_i2s (peer));
+        path = path_new (2);
+        path->peers[1] = neighbor->id;
+        GNUNET_PEER_change_rc (neighbor->id, 1);
+        GNUNET_assert (NULL == neighbor->core_mq);
+        neighbor->core_mq = mq;
+    }
+    path->peers[0] = myid;
+    GNUNET_PEER_change_rc (myid, 1);
+    GCP_add_path (neighbor, path, GNUNET_YES);
+
+    /* Create the connections hashmap */
+    GNUNET_assert (NULL == neighbor->connections);
+    neighbor->connections = GNUNET_CONTAINER_multishortmap_create (16,
+                                                                   GNUNET_YES);
+    GNUNET_STATISTICS_update (stats,
+                              "# peers",
+                              1,
+                              GNUNET_NO);
 
-  GNUNET_STATISTICS_update (stats,
-                            "# peers",
-                            1,
-                            GNUNET_NO);
+    if ( (NULL != GCP_get_tunnel (neighbor)) &&
+            (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
+    {
+        GCP_connect (neighbor);
+    }
+    GCC_check_connections ();
 
-  if ( (NULL != GCP_get_tunnel (neighbor)) &&
-       (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
-    GCP_connect (neighbor);
-  GCC_check_connections ();
+    return neighbor;
 }
 
 
 /**
  * Method called whenever a peer disconnects.
  *
- * @param cls closure
- * @param peer peer identity this notification is about
+ * @param cls Core closure (unused).
+ * @param peer Peer identity this notification is about.
+ * @param internal_cls Internal closure (CadetPeer struct).
  */
 static void
-core_disconnect (void *cls,
-                 const struct GNUNET_PeerIdentity *peer)
-{
-  struct CadetPeer *p;
-  struct CadetPeerPath *direct_path;
-  char own_id[16];
-
-  GCC_check_connections ();
-  strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
-  own_id[15] = '\0';
-  p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
-  if (NULL == p)
-  {
-    GNUNET_break (GNUNET_YES == in_shutdown);
-    return;
-  }
-  if (myid == p->id)
-    LOG (GNUNET_ERROR_TYPE_INFO,
-         "DISCONNECTED %s (self)\n",
-         own_id);
-  else
-    LOG (GNUNET_ERROR_TYPE_INFO,
-         "DISCONNECTED %s <= %s\n",
-         own_id, GNUNET_i2s (peer));
-  direct_path = pop_direct_path (p);
-  GNUNET_CONTAINER_multihashmap_iterate (p->connections,
-                                         &notify_broken,
-                                         p);
-  GNUNET_CONTAINER_multihashmap_destroy (p->connections);
-  p->connections = NULL;
-  if (NULL != p->core_transmit)
-  {
-    GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
-    p->core_transmit = NULL;
-    p->tmt_time.abs_value_us = 0;
-  }
-  GNUNET_STATISTICS_update (stats,
-                            "# peers",
-                            -1,
-                            GNUNET_NO);
-  path_destroy (direct_path);
-  GCC_check_connections ();
-}
-
-
-/**
- * Functions to handle messages from core
- */
-static struct GNUNET_CORE_MessageHandler core_handlers[] = {
-  {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
-  {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
-    sizeof (struct GNUNET_CADET_ConnectionACK)},
-  {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
-    sizeof (struct GNUNET_CADET_ConnectionBroken)},
-  {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
-    sizeof (struct GNUNET_CADET_ConnectionDestroy)},
-  {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
-    sizeof (struct GNUNET_CADET_ACK)},
-  {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
-    sizeof (struct GNUNET_CADET_Poll)},
-  {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
-  {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
-  {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0},
-  {NULL, 0, 0}
-};
+core_disconnect_handler (void *cls,
+                         const struct GNUNET_PeerIdentity *peer,
+                         void *internal_cls)
+{
+    struct CadetPeer *p = internal_cls;
+    struct CadetPeerPath *direct_path;
+    char own_id[16];
+
+    GCC_check_connections ();
+    strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
+    own_id[15] = '\0';
+    if (myid == p->id)
+    {
+        LOG (GNUNET_ERROR_TYPE_INFO,
+             "DISCONNECTED %s (self)\n",
+             own_id);
+    }
+    else
+    {
+        LOG (GNUNET_ERROR_TYPE_INFO,
+             "DISCONNECTED %s <= %s\n",
+             own_id, GNUNET_i2s (peer));
+        p->core_mq = NULL;
+    }
+    direct_path = pop_direct_path (p);
+    if (NULL != p->connections)
+    {
+      GNUNET_CONTAINER_multishortmap_iterate (p->connections,
+                                              &notify_broken,
+                                              p);
+      GNUNET_CONTAINER_multishortmap_destroy (p->connections);
+      p->connections = NULL;
+    }
+    GNUNET_STATISTICS_update (stats,
+                              "# peers",
+                              -1,
+                              GNUNET_NO);
+    path_destroy (direct_path);
+    GCC_check_connections ();
+}
 
 
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+
 /**
- * To be called on core init/fail.
+ * Check if the create_connection message has the appropriate size.
  *
- * @param cls Closure (config)
- * @param identity the public identity of this peer
+ * @param cls Closure (unused).
+ * @param msg Message to check.
+ *
+ * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
  */
-static void
-core_init (void *cls,
-           const struct GNUNET_PeerIdentity *identity)
+static int
+check_create (void *cls, const struct GNUNET_CADET_ConnectionCreateMessage *msg)
 {
-  const struct GNUNET_CONFIGURATION_Handle *c = cls;
-  static int i = 0;
+    uint16_t size;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
-  if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
-  {
-    LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
-    LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
-    LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
-    GNUNET_CORE_disconnect (core_handle);
-    core_handle = GNUNET_CORE_connect (c, /* Main configuration */
-                                       NULL,      /* Closure passed to CADET 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 (10 < i++)
-      GNUNET_assert (0);
-  }
-  GML_start ();
+    size = ntohs (msg->header.size);
+    if (size < sizeof (*msg))
+    {
+        GNUNET_break_op (0);
+        return GNUNET_NO;
+    }
+    return GNUNET_YES;
+}
+
+/**
+ * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
+ *
+ * @param cls Closure (CadetPeer for neighbor that sent the message).
+ * @param msg Message itself.
+ */
+static void
+handle_create (void *cls, const struct GNUNET_CADET_ConnectionCreateMessage *msg)
+{
+    struct CadetPeer *peer = cls;
+    GCC_handle_create (peer, msg);
 }
 
 
 /**
-  * Core callback to write a pre-constructed data packet to core buffer
-  *
-  * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
-  * @param size Number of bytes available in buf.
-  * @param buf Where the to write the message.
-  *
-  * @return number of bytes written to buf
-  */
-static size_t
-send_core_data_raw (void *cls, size_t size, void *buf)
+ * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
+ *
+ * @param cls Closure (CadetPeer for neighbor that sent the message).
+ * @param msg Message itself.
+ */
+static void
+handle_confirm (void *cls, const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
 {
-  struct GNUNET_MessageHeader *msg = cls;
-  size_t total_size;
+    struct CadetPeer *peer = cls;
+    GCC_handle_confirm (peer, msg);
+}
 
-  GNUNET_assert (NULL != msg);
-  total_size = ntohs (msg->size);
 
-  if (total_size > size)
-  {
-    GNUNET_break (0);
-    return 0;
-  }
-  memcpy (buf, msg, total_size);
-  GNUNET_free (cls);
-  return total_size;
+/**
+ * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
+ *
+ * @param cls Closure (CadetPeer for neighbor that sent the message).
+ * @param msg Message itself.
+ */
+static void
+handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
+{
+    struct CadetPeer *peer = cls;
+    GCC_handle_broken (peer, msg);
 }
 
 
 /**
- * Function to send a create connection message to a peer.
+ * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
  *
- * @param c Connection to create.
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
+ * @param cls Closure (CadetPeer for neighbor that sent the message).
+ * @param msg Message itself.
  */
-static size_t
-send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
+static void
+handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
 {
-  struct GNUNET_CADET_ConnectionCreate *msg;
-  struct GNUNET_PeerIdentity *peer_ptr;
-  const struct CadetPeerPath *p = GCC_get_path (c);
-  size_t size_needed;
-  int i;
+    struct CadetPeer *peer = cls;
+    GCC_handle_destroy (peer, msg);
+}
 
-  if (NULL == p)
-    return 0;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
-  size_needed =
-      sizeof (struct GNUNET_CADET_ConnectionCreate) +
-      p->length * sizeof (struct GNUNET_PeerIdentity);
+/**
+ * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK
+ *
+ * @param cls Closure (CadetPeer for neighbor that sent the message).
+ * @param msg Message itself.
+ */
+static void
+handle_ack (void *cls, const struct GNUNET_CADET_ConnectionEncryptedAckMessage *msg)
+{
+    struct CadetPeer *peer = cls;
+    GCC_handle_ack (peer, msg);
+}
 
-  if (size < size_needed || NULL == buf)
-  {
-    GNUNET_break (0);
-    return 0;
-  }
-  msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
-  msg->header.size = htons (size_needed);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
-  msg->cid = *GCC_get_id (c);
 
-  peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
-  for (i = 0; i < p->length; i++)
-  {
-    GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
-  }
+/**
+ * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL
+ *
+ * @param cls Closure (CadetPeer for neighbor that sent the message).
+ * @param msg Message itself.
+ */
+static void
+handle_poll (void *cls, const struct GNUNET_CADET_ConnectionHopByHopPollMessage *msg)
+{
+    struct CadetPeer *peer = cls;
+    GCC_handle_poll (peer, msg);
+}
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "CONNECTION CREATE (%u bytes long) sent!\n",
-       size_needed);
-  return size_needed;
+
+/**
+ * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
+ *
+ * @param cls Closure (CadetPeer for neighbor that sent the message).
+ * @param msg Message itself.
+ */
+static void
+handle_kx (void *cls, const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
+{
+    struct CadetPeer *peer = cls;
+    GCC_handle_kx (peer, msg);
 }
 
 
 /**
- * Creates a path ack message in buf and frees all unused resources.
+ * Check if the encrypted message has the appropriate size.
  *
- * @param c Connection to send an ACK on.
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
+ * @param cls Closure (unused).
+ * @param msg Message to check.
  *
- * @return number of bytes written to buf
+ * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
  */
-static size_t
-send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
+static int
+check_encrypted (void *cls, const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
 {
-  struct GNUNET_CADET_ConnectionACK *msg = buf;
+    uint16_t size;
+    uint16_t minimum_size;
+
+    size = ntohs (msg->header.size);
+    minimum_size = sizeof (struct GNUNET_CADET_TunnelEncryptedMessage)
+                   + sizeof (struct GNUNET_MessageHeader);
+
+    if (size < minimum_size)
+    {
+        GNUNET_break_op (0);
+        return GNUNET_NO;
+    }
+    return GNUNET_YES;
+}
+
+/**
+ * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
+ *
+ * @param cls Closure (CadetPeer for neighbor that sent the message).
+ * @param msg Message itself.
+ */
+static void
+handle_encrypted (void *cls, const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
+{
+    struct CadetPeer *peer = cls;
+    GCC_handle_encrypted (peer, msg);
+}
+
+
+/**
+ * To be called on core init/fail.
+ *
+ * @param cls Closure (config)
+ * @param identity The public identity of this peer.
+ */
+static void
+core_init_notify (void *cls,
+                  const struct GNUNET_PeerIdentity *identity);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
-  if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
-  {
-    GNUNET_break (0);
-    return 0;
-  }
-  msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
-  msg->cid = *GCC_get_id (c);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
-  return sizeof (struct GNUNET_CADET_ConnectionACK);
+static void
+connect_to_core (const struct GNUNET_CONFIGURATION_Handle *c)
+{
+    struct GNUNET_MQ_MessageHandler core_handlers[] = {
+        GNUNET_MQ_hd_var_size (create,
+                               GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
+                               struct GNUNET_CADET_ConnectionCreateMessage,
+                               NULL),
+        GNUNET_MQ_hd_fixed_size (confirm,
+                                 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
+                                 struct GNUNET_CADET_ConnectionCreateAckMessage,
+                                 NULL),
+        GNUNET_MQ_hd_fixed_size (broken,
+                                GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
+                                struct GNUNET_CADET_ConnectionBrokenMessage,
+                                NULL),
+        GNUNET_MQ_hd_fixed_size (destroy,
+                                 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
+                                 struct GNUNET_CADET_ConnectionDestroyMessage,
+                                 NULL),
+        GNUNET_MQ_hd_fixed_size (ack,
+                                 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK,
+                                 struct GNUNET_CADET_ConnectionEncryptedAckMessage,
+                                 NULL),
+        GNUNET_MQ_hd_fixed_size (poll,
+                                 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL,
+                                 struct GNUNET_CADET_ConnectionHopByHopPollMessage,
+                                 NULL),
+        GNUNET_MQ_hd_fixed_size (kx,
+                                 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
+                                 struct GNUNET_CADET_TunnelKeyExchangeMessage,
+                                 NULL),
+        GNUNET_MQ_hd_var_size (encrypted,
+                               GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
+                               struct GNUNET_CADET_TunnelEncryptedMessage,
+                               NULL),
+        GNUNET_MQ_handler_end ()
+    };
+    core_handle = GNUNET_CORE_connect (c, NULL,
+                                       &core_init_notify,
+                                       &core_connect_handler,
+                                       &core_disconnect_handler,
+                                       core_handlers);
+}
+
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+
+/**
+ * To be called on core init/fail.
+ *
+ * @param cls Closure (config)
+ * @param identity The public identity of this peer.
+ */
+static void
+core_init_notify (void *cls,
+                  const struct GNUNET_PeerIdentity *core_identity)
+{
+    const struct GNUNET_CONFIGURATION_Handle *c = cls;
+
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
+    if (0 != memcmp (core_identity, &my_full_id, sizeof (my_full_id)))
+    {
+        LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
+        LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (core_identity));
+        LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
+        GNUNET_CORE_disconnect (core_handle);
+        connect_to_core (c);
+        return;
+    }
+    GML_start ();
 }
 
 
@@ -697,40 +706,60 @@ send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
  * @param q Queued message
  *
  * @return CORE priority to use.
+ *
+ * FIXME make static
+ * FIXME use when sending
  */
-static enum GNUNET_CORE_Priority
+enum GNUNET_CORE_Priority
 get_priority (struct CadetPeerQueue *q)
 {
-  enum GNUNET_CORE_Priority low;
-  enum GNUNET_CORE_Priority high;
+    enum GNUNET_CORE_Priority low;
+    enum GNUNET_CORE_Priority high;
 
-  if (NULL == q)
-  {
-    GNUNET_break (0);
-    return GNUNET_CORE_PRIO_BACKGROUND;
-  }
+    if (NULL == q)
+    {
+        GNUNET_break (0);
+        return GNUNET_CORE_PRIO_BACKGROUND;
+    }
 
-  /* Relayed traffic has lower priority, our own traffic has higher */
-  if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
-  {
-    low = GNUNET_CORE_PRIO_BEST_EFFORT;
-    high = GNUNET_CORE_PRIO_URGENT;
-  }
-  else
-  {
-    low = GNUNET_CORE_PRIO_URGENT;
-    high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
-  }
+    /* Relayed traffic has lower priority, our own traffic has higher */
+    if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->c_fwd))
+    {
+        low = GNUNET_CORE_PRIO_BEST_EFFORT;
+        high = GNUNET_CORE_PRIO_URGENT;
+    }
+    else
+    {
+        low = GNUNET_CORE_PRIO_URGENT;
+        high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
+    }
 
-  /* Bulky payload has lower priority, control traffic has higher. */
-  if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
-      || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
-    return low;
-  else
+    /* Bulky payload has lower priority, control traffic has higher. */
+    if (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED == q->type)
+        return low;
     return high;
 }
 
 
+/**
+ * Cancel all messages queued to CORE MQ towards this peer.
+ *
+ * @param peer Peer towards which to cancel all messages.
+ */
+static void
+cancel_queued_messages (struct CadetPeer *peer)
+{
+    while (NULL != peer->q_head)
+    {
+        struct CadetPeerQueue *q;
+
+        q = peer->q_head;
+        call_peer_cont (q, GNUNET_NO);
+        GNUNET_free (q);
+    }
+}
+
+
 /**
  * Destroy the peer_info and free any allocated resources linked to it
  *
@@ -740,65 +769,57 @@ get_priority (struct CadetPeerQueue *q)
 static int
 peer_destroy (struct CadetPeer *peer)
 {
-  struct GNUNET_PeerIdentity id;
-  struct CadetPeerPath *p;
-  struct CadetPeerPath *nextp;
+    struct GNUNET_PeerIdentity id;
+    struct CadetPeerPath *p;
+    struct CadetPeerPath *nextp;
 
-  GNUNET_PEER_resolve (peer->id, &id);
-  GNUNET_PEER_change_rc (peer->id, -1);
+    GNUNET_PEER_resolve (peer->id, &id);
+    GNUNET_PEER_change_rc (peer->id, -1);
 
-  LOG (GNUNET_ERROR_TYPE_INFO,
-       "destroying peer %s\n",
-       GNUNET_i2s (&id));
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "destroying peer %s\n",
+         GNUNET_i2s (&id));
 
-  if (GNUNET_YES !=
-      GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
-  {
-    GNUNET_break (0);
-    LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
-  }
-  GCP_stop_search (peer);
-  p = peer->path_head;
-  while (NULL != p)
-  {
-    nextp = p->next;
-    GNUNET_CONTAINER_DLL_remove (peer->path_head,
-                                 peer->path_tail,
-                                 p);
-    path_destroy (p);
-    p = nextp;
-  }
-  if (NULL != peer->tunnel)
-    GCT_destroy_empty (peer->tunnel);
-  if (NULL != peer->connections)
-  {
-    GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
-    GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
-  }
-  if (NULL != peer->hello_offer)
-  {
-    GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
-    peer->hello_offer = NULL;
-  }
-  if (NULL != peer->connectivity_suggestion)
-  {
-    GNUNET_ATS_connectivity_suggest_cancel (peer->connectivity_suggestion);
-    peer->connectivity_suggestion = NULL;
-  }
-  while (NULL != peer->queue_head)
-  {
-    GCP_queue_destroy (peer->queue_head, GNUNET_YES, GNUNET_NO, 0);
-  }
-  if (NULL != peer->core_transmit)
-  {
-    GNUNET_break (0); /* GCP_queue_destroy should've cancelled it! */
-    GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
-    peer->core_transmit = NULL;
-  }
+    if (GNUNET_YES !=
+            GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
+    {
+        GNUNET_break (0);
+        LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
+    }
+    GCP_stop_search (peer);
+    p = peer->path_head;
+    while (NULL != p)
+    {
+        nextp = p->next;
+        GNUNET_CONTAINER_DLL_remove (peer->path_head,
+                                     peer->path_tail,
+                                     p);
+        path_destroy (p);
+        p = nextp;
+    }
+    if (NULL != peer->tunnel)
+        GCT_destroy_empty (peer->tunnel);
+    if (NULL != peer->connections)
+    {
+        GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (peer->connections));
+        GNUNET_CONTAINER_multishortmap_destroy (peer->connections);
+        peer->connections = NULL;
+    }
+    if (NULL != peer->hello_offer)
+    {
+        GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
+        peer->hello_offer = NULL;
+    }
+    if (NULL != peer->connectivity_suggestion)
+    {
+        GNUNET_ATS_connectivity_suggest_cancel (peer->connectivity_suggestion);
+        peer->connectivity_suggestion = NULL;
+    }
+    cancel_queued_messages (peer);
 
-  GNUNET_free_non_null (peer->hello);
-  GNUNET_free (peer);
-  return GNUNET_OK;
+    GNUNET_free_non_null (peer->hello);
+    GNUNET_free (peer);
+    return GNUNET_OK;
 }
 
 
@@ -816,16 +837,16 @@ shutdown_peer (void *cls,
                const struct GNUNET_PeerIdentity *key,
                void *value)
 {
-  struct CadetPeer *p = value;
-  struct CadetTunnel *t = p->tunnel;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  shutting down %s\n", GCP_2s (p));
-  if (NULL != t)
-    GCT_destroy (t);
-  p->tunnel = NULL;
-  peer_destroy (p);
-  return GNUNET_YES;
-}
+    struct CadetPeer *p = value;
+    struct CadetTunnel *t = p->tunnel;
 
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  shutting down %s\n", GCP_2s (p));
+    if (NULL != t)
+        GCT_destroy (t);
+    p->tunnel = NULL;
+    peer_destroy (p);
+    return GNUNET_YES;
+}
 
 
 /**
@@ -838,9 +859,9 @@ shutdown_peer (void *cls,
 static int
 is_searching (const struct CadetPeer *peer)
 {
-  return ( (NULL == peer->search_h) &&
-           (NULL == peer->search_delayed) ) ?
-    GNUNET_NO : GNUNET_YES;
+    return ( (NULL == peer->search_h) &&
+             (NULL == peer->search_delayed) ) ?
+           GNUNET_NO : GNUNET_YES;
 }
 
 
@@ -852,12 +873,12 @@ is_searching (const struct CadetPeer *peer)
 static void
 delayed_search (void *cls)
 {
-  struct CadetPeer *peer = cls;
+    struct CadetPeer *peer = cls;
 
-  peer->search_delayed = NULL;
-  GCC_check_connections ();
-  GCP_start_search (peer);
-  GCC_check_connections ();
+    peer->search_delayed = NULL;
+    GCC_check_connections ();
+    GCP_start_search (peer);
+    GCC_check_connections ();
 }
 
 
@@ -870,16 +891,16 @@ delayed_search (void *cls)
 static int
 peer_is_used (struct CadetPeer *peer)
 {
-  struct CadetPeerPath *p;
+    struct CadetPeerPath *p;
 
-  if (NULL != peer->tunnel)
-    return GNUNET_YES;
+    if (NULL != peer->tunnel)
+        return GNUNET_YES;
 
-  for (p = peer->path_head; NULL != p; p = p->next)
-  {
-    if (p->length < 3)
-      return GNUNET_YES;
-  }
+    for (p = peer->path_head; NULL != p; p = p->next)
+    {
+        if (p->length < 3)
+            return GNUNET_YES;
+    }
     return GNUNET_NO;
 }
 
@@ -896,17 +917,17 @@ peer_get_oldest (void *cls,
                  const struct GNUNET_PeerIdentity *key,
                  void *value)
 {
-  struct CadetPeer *p = value;
-  struct GNUNET_TIME_Absolute *abs = cls;
+    struct CadetPeer *p = value;
+    struct GNUNET_TIME_Absolute *abs = cls;
 
-  /* Don't count active peers */
-  if (GNUNET_YES == peer_is_used (p))
-    return GNUNET_YES;
+    /* Don't count active peers */
+    if (GNUNET_YES == peer_is_used (p))
+        return GNUNET_YES;
 
-  if (abs->abs_value_us < p->last_contact.abs_value_us)
-    abs->abs_value_us = p->last_contact.abs_value_us;
+    if (abs->abs_value_us < p->last_contact.abs_value_us)
+        abs->abs_value_us = p->last_contact.abs_value_us;
 
-  return GNUNET_YES;
+    return GNUNET_YES;
 }
 
 
@@ -922,18 +943,18 @@ peer_timeout (void *cls,
               const struct GNUNET_PeerIdentity *key,
               void *value)
 {
-  struct CadetPeer *p = value;
-  struct GNUNET_TIME_Absolute *abs = cls;
+    struct CadetPeer *p = value;
+    struct GNUNET_TIME_Absolute *abs = cls;
 
-  LOG (GNUNET_ERROR_TYPE_WARNING,
-       "peer %s timeout\n", GNUNET_i2s (key));
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "peer %s timeout\n", GNUNET_i2s (key));
 
-  if (p->last_contact.abs_value_us == abs->abs_value_us &&
-      GNUNET_NO == peer_is_used (p))
-  {
-    peer_destroy (p);
-    return GNUNET_NO;
-  }
+    if (p->last_contact.abs_value_us == abs->abs_value_us &&
+            GNUNET_NO == peer_is_used (p))
+    {
+        peer_destroy (p);
+        return GNUNET_NO;
+    }
     return GNUNET_YES;
 }
 
@@ -944,16 +965,16 @@ peer_timeout (void *cls,
 static void
 peer_delete_oldest (void)
 {
-  struct GNUNET_TIME_Absolute abs;
+    struct GNUNET_TIME_Absolute abs;
 
-  abs = GNUNET_TIME_UNIT_FOREVER_ABS;
+    abs = GNUNET_TIME_UNIT_FOREVER_ABS;
 
-  GNUNET_CONTAINER_multipeermap_iterate (peers,
-                                         &peer_get_oldest,
-                                         &abs);
-  GNUNET_CONTAINER_multipeermap_iterate (peers,
-                                         &peer_timeout,
-                                         &abs);
+    GNUNET_CONTAINER_multipeermap_iterate (peers,
+                                           &peer_get_oldest,
+                                           &abs);
+    GNUNET_CONTAINER_multipeermap_iterate (peers,
+                                           &peer_timeout,
+                                           &abs);
 }
 
 
@@ -967,86 +988,27 @@ peer_delete_oldest (void)
 static struct CadetPeerPath *
 peer_get_best_path (const struct CadetPeer *peer)
 {
-  struct CadetPeerPath *best_p;
-  struct CadetPeerPath *p;
-  unsigned int best_cost;
-  unsigned int cost;
+    struct CadetPeerPath *best_p;
+    struct CadetPeerPath *p;
+    unsigned int best_cost;
+    unsigned int cost;
 
-  best_cost = UINT_MAX;
-  best_p = NULL;
-  for (p = peer->path_head; NULL != p; p = p->next)
-  {
-    if (GNUNET_NO == path_is_valid (p))
-      continue; /* Don't use invalid paths. */
-    if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
-      continue; /* If path is already in use, skip it. */
-
-    if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
+    best_cost = UINT_MAX;
+    best_p = NULL;
+    for (p = peer->path_head; NULL != p; p = p->next)
     {
-      best_cost = cost;
-      best_p = p;
-    }
-  }
-  return best_p;
-}
-
-
-/**
- * Is this queue element sendable?
- *
- * - All management traffic is always sendable.
- * - For payload traffic, check the connection flow control.
- *
- * @param q Queue element to inspect.
- * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
- */
-static int
-queue_is_sendable (struct CadetPeerQueue *q)
-{
-  /* Is PID-independent? */
-  switch (q->type)
-  {
-    case GNUNET_MESSAGE_TYPE_CADET_ACK:
-    case GNUNET_MESSAGE_TYPE_CADET_POLL:
-    case GNUNET_MESSAGE_TYPE_CADET_KX:
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
-      return GNUNET_YES;
-
-    case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
-    case GNUNET_MESSAGE_TYPE_CADET_AX:
-      break;
-
-    default:
-      GNUNET_break (0);
-  }
+        if (GNUNET_NO == path_is_valid (p))
+            continue; /* Don't use invalid paths. */
+        if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
+            continue; /* If path is already in use, skip it. */
 
-  return GCC_is_sendable (q->c, q->fwd);
-}
-
-
-/**
- * Get first sendable message.
- *
- * @param peer The destination peer.
- *
- * @return First transmittable message, if any. Otherwise, NULL.
- */
-static struct CadetPeerQueue *
-peer_get_first_message (const struct CadetPeer *peer)
-{
-  struct CadetPeerQueue *q;
-
-  for (q = peer->queue_head; NULL != q; q = q->next)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
-    if (queue_is_sendable (q))
-      return q;
-  }
-
-  return NULL;
+        if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
+        {
+            best_cost = cost;
+            best_p = p;
+        }
+    }
+    return best_p;
 }
 
 
@@ -1061,44 +1023,31 @@ peer_get_first_message (const struct CadetPeer *peer)
 static void
 search_handler (void *cls, const struct CadetPeerPath *path)
 {
-  struct CadetPeer *peer = cls;
-  unsigned int connection_count;
+    struct CadetPeer *peer = cls;
+    unsigned int connection_count;
 
-  GCC_check_connections ();
-  GCP_add_path_to_all (path, GNUNET_NO);
-
-  /* Count connections */
-  connection_count = GCT_count_connections (peer->tunnel);
-
-  /* If we already have our minimum (or more) connections, it's enough */
-  if (CONNECTIONS_PER_TUNNEL <= connection_count)
-  {
     GCC_check_connections ();
-    return;
-  }
+    GCP_add_path_to_all (path, GNUNET_NO);
 
-  if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
-    GCP_connect (peer);
-  }
-  GCC_check_connections ();
-}
+    /* Count connections */
+    connection_count = GCT_count_connections (peer->tunnel);
 
+    /* If we already have our minimum (or more) connections, it's enough */
+    if (CONNECTIONS_PER_TUNNEL <= connection_count)
+    {
+        GCC_check_connections ();
+        return;
+    }
 
-/**
- * Adjust core requested size to accomodate an ACK.
- *
- * @param message_size Requested size.
- *
- * @return Size enough to fit @c message_size and an ACK.
- */
-static size_t
-get_core_size (size_t message_size)
-{
-  return message_size + sizeof (struct GNUNET_CADET_ACK);
+    if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
+    {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
+        GCP_connect (peer);
+    }
+    GCC_check_connections ();
 }
 
+
 /**
  * Test if a message type is connection management traffic
  * or regular payload traffic.
@@ -1110,84 +1059,8 @@ get_core_size (size_t message_size)
 static int
 is_connection_management (uint16_t type)
 {
-  return type == GNUNET_MESSAGE_TYPE_CADET_ACK ||
-         type == GNUNET_MESSAGE_TYPE_CADET_POLL;
-}
-
-
-/**
- * Fill a core buffer with the appropriate data for the queued message.
- *
- * @param queue Queue element for the message.
- * @param buf Core buffer to fill.
- * @param size Size remaining in @c buf.
- * @param[out] pid In case its an encrypted payload, set payload.
- *
- * @return Bytes written to @c buf.
- */
-static size_t
-fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
-{
-  struct CadetConnection *c = queue->c;
-  size_t msg_size;
-
-  switch (queue->type)
-  {
-    case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
-      *pid = GCC_get_pid (queue->c, queue->fwd);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  otr payload ID %u\n", *pid);
-      msg_size = send_core_data_raw (queue->cls, size, buf);
-      ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
-      break;
-    case GNUNET_MESSAGE_TYPE_CADET_AX:
-      *pid = GCC_get_pid (queue->c, queue->fwd);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  ax payload ID %u\n", *pid);
-      msg_size = send_core_data_raw (queue->cls, size, buf);
-      ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
-      break;
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
-    case GNUNET_MESSAGE_TYPE_CADET_KX:
-    case GNUNET_MESSAGE_TYPE_CADET_ACK:
-    case GNUNET_MESSAGE_TYPE_CADET_POLL:
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  raw %s\n", GC_m2s (queue->type));
-      msg_size = send_core_data_raw (queue->cls, size, buf);
-      break;
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  path create\n");
-      if (GCC_is_origin (c, GNUNET_YES))
-        msg_size = send_core_connection_create (c, size, buf);
-      else
-        msg_size = send_core_data_raw (queue->cls, size, buf);
-      break;
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  path ack\n");
-      if (GCC_is_origin (c, GNUNET_NO) ||
-          GCC_is_origin (c, GNUNET_YES))
-      {
-        msg_size = send_core_connection_ack (c, size, buf);
-      }
-      else
-      {
-        msg_size = send_core_data_raw (queue->cls, size, buf);
-      }
-      break;
-    case GNUNET_MESSAGE_TYPE_CADET_DATA:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
-      /* This should be encapsulted */
-      msg_size = 0;
-      GNUNET_assert (0);
-      break;
-    default:
-      GNUNET_break (0);
-      LOG (GNUNET_ERROR_TYPE_WARNING, "  type unknown: %u\n", queue->type);
-      msg_size = 0;
-  }
-
-  GNUNET_assert (size >= msg_size);
-
-  return msg_size;
+    return type == GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK ||
+           type == GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL;
 }
 
 
@@ -1195,182 +1068,18 @@ fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
  * Debug function should NEVER return true in production code, useful to
  * simulate losses for testcases.
  *
- * @param q Queue handle with info about the message.
- *
  * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
  */
 static int
-should_I_drop (struct CadetPeerQueue *q)
-{
-  if (0 == drop_percent)
-    return GNUNET_NO;
-
-  if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
-    return GNUNET_YES;
-
-  return GNUNET_NO;
-}
-
-
-/**
- * Core callback to write a queued packet to core buffer
- *
- * @param cls Closure (peer info).
- * @param size Number of bytes available in buf.
- * @param buf Where the to write the message.
- *
- * @return number of bytes written to buf
- */
-static size_t
-queue_send (void *cls, size_t size, void *buf)
+should_I_drop (void)
 {
-  struct CadetPeer *peer = cls;
-  struct CadetConnection *c;
-  struct CadetPeerQueue *queue;
-  struct GNUNET_TIME_Relative core_wait_time;
-  const char *wait_s;
-  const struct GNUNET_PeerIdentity *dst_id;
-  size_t msg_size;
-  size_t total_size;
-  size_t rest;
-  char *dst;
-  uint32_t pid;
+    if (0 == drop_percent)
+        return GNUNET_NO;
 
-  GCC_check_connections ();
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
-       GCP_2s (peer), size);
+    if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
+        return GNUNET_YES;
 
-  /* Sanity checking */
-  if (NULL == buf || 0 == size)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  not allowed/\n");
-    if (GNUNET_NO == in_shutdown)
-    {
-      queue = peer_get_first_message (peer);
-      if (NULL == queue)
-      {
-        peer->core_transmit = NULL;
-        peer->tmt_time.abs_value_us = 0;
-        GCC_check_connections ();
-        return 0;
-      }
-      dst_id = GNUNET_PEER_resolve2 (peer->id);
-      peer->core_transmit =
-          GNUNET_CORE_notify_transmit_ready (core_handle,
-                                             GNUNET_NO, get_priority (queue),
-                                             GNUNET_TIME_UNIT_FOREVER_REL,
-                                             dst_id,
-                                             get_core_size (queue->size),
-                                             &queue_send,
-                                             peer);
-      peer->tmt_time = GNUNET_TIME_absolute_get ();
-    }
-    else
-    {
-      peer->core_transmit = NULL;
-      peer->tmt_time.abs_value_us = 0;
-    }
-    GCC_check_connections ();
-    return 0;
-  }
-
-  /* Init */
-  rest = size;
-  total_size = 0;
-  dst = (char *) buf;
-  pid = 0;
-  peer->core_transmit = NULL;
-  queue = peer_get_first_message (peer);
-  if (NULL == queue)
-  {
-    GNUNET_break (0); /* Core tmt_rdy should've been canceled */
-    peer->tmt_time.abs_value_us = 0;
-    return 0;
-  }
-  core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
-  wait_s = GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES);
-  if (core_wait_time.rel_value_us >= 1000000)
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-         " %s: core wait time %s (> 1 second) for %u bytes\n",
-         GCP_2s (peer), wait_s, queue->size);
-  }
-  peer->tmt_time.abs_value_us = 0;
-
-  /* Copy all possible messages to the core buffer */
-  while (NULL != queue && rest >= queue->size)
-  {
-    c = queue->c;
-
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  on conn %s %s\n",
-         GCC_2s (c), GC_f2s(queue->fwd));
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  size %u ok (%u/%u)\n",
-         queue->size, total_size, size);
-
-    msg_size = fill_buf (queue, (void *) dst, size, &pid);
-
-    if (should_I_drop (queue))
-    {
-      LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
-           GC_m2s (queue->type), GC_m2s (queue->payload_type),
-           queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
-      msg_size = 0;
-    }
-    else
-    {
-      LOG (GNUNET_ERROR_TYPE_INFO,
-           ">>> %s (%s %4u) on conn %s (%p) %s [%5u], after %s\n",
-           GC_m2s (queue->type), GC_m2s (queue->payload_type),
-           queue->payload_id, GCC_2s (c), c,
-           GC_f2s (queue->fwd), msg_size, wait_s);
-    }
-    total_size += msg_size;
-    rest -= msg_size;
-    dst = &dst[msg_size];
-    msg_size = 0;
-
-    /* Free queue, but cls was freed by send_core_* in fill_buf. */
-    (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
-
-    /* Next! */
-    queue = peer_get_first_message (peer);
-  }
-
-  /* If more data in queue, send next */
-  if (NULL != queue)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  more data! (%u)\n", queue->size);
-    if (NULL == peer->core_transmit)
-    {
-      dst_id = GNUNET_PEER_resolve2 (peer->id);
-      peer->core_transmit =
-          GNUNET_CORE_notify_transmit_ready (core_handle,
-                                             GNUNET_NO, get_priority (queue),
-                                             GNUNET_TIME_UNIT_FOREVER_REL,
-                                             dst_id,
-                                             get_core_size (queue->size),
-                                             &queue_send,
-                                             peer);
-      peer->tmt_time = GNUNET_TIME_absolute_get ();
-      queue->start_waiting = GNUNET_TIME_absolute_get ();
-    }
-    else
-    {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "*   tmt rdy called somewhere else\n");
-    }
-//     GCC_start_poll (); FIXME needed?
-  }
-  else
-  {
-//     GCC_stop_poll(); FIXME needed?
-  }
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  return %d\n", total_size);
-  queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
-  GCC_check_connections ();
-  return total_size;
+    return GNUNET_NO;
 }
 
 
@@ -1378,417 +1087,191 @@ queue_send (void *cls, size_t size, void *buf)
 /********************************    API    ***********************************/
 /******************************************************************************/
 
-
 /**
- * Free a transmission that was already queued with all resources
- * associated to the request.
+ * Call the continuation after a message has been sent or dropped.
  *
- * If connection was marked to be destroyed, and this was the last queued
- * message on it, the connection will be free'd as a result.
+ * This funcion removes the message from the queue.
  *
- * @param queue Queue handler to cancel.
- * @param clear_cls Is it necessary to free associated cls?
- * @param sent Was it really sent? (Could have been canceled)
- * @param pid PID, if relevant (was sent and was a payload message).
- *
- * @return #GNUNET_YES if connection was destroyed as a result,
- *         #GNUNET_NO otherwise.
+ * @param q Queue handle.
+ * @param sent #GNUNET_YES if was sent to CORE, #GNUNET_NO if dropped.
  */
-int
-GCP_queue_destroy (struct CadetPeerQueue *queue,
-                   int clear_cls,
-                   int sent,
-                   uint32_t pid)
-{
-  struct CadetPeer *peer;
-  int connection_destroyed;
-
-  GCC_check_connections ();
-  peer = queue->peer;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
-  if (GNUNET_YES == clear_cls)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
-    switch (queue->type)
+static void
+call_peer_cont (struct CadetPeerQueue *q, int sent)
+{
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " core mq just sent %s\n", GC_m2s (q->type));
+    if (NULL != q->cont)
     {
-      case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
-        LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
-        /* fall through */
-      case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
-      case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
-      case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
-      case GNUNET_MESSAGE_TYPE_CADET_KX:
-      case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
-      case GNUNET_MESSAGE_TYPE_CADET_AX:
-      case GNUNET_MESSAGE_TYPE_CADET_ACK:
-      case GNUNET_MESSAGE_TYPE_CADET_POLL:
-        GNUNET_free_non_null (queue->cls);
-        break;
-
-      default:
-        GNUNET_break (0);
-        LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
-             GC_m2s (queue->type));
+        struct GNUNET_TIME_Relative wait_time;
+
+        wait_time = GNUNET_TIME_absolute_get_duration (q->queue_timestamp);
+        LOG (GNUNET_ERROR_TYPE_DEBUG,
+             " calling callback on %s after %s\n",
+             GCC_2s (q->c),
+             GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
+        q->cont (q->cont_cls,
+                 q->c, q->c_fwd, sent,
+                 q->type,
+                 q->payload_type,
+                 q->payload_id,
+                 q->size, wait_time);
+       q->cont = NULL;
     }
-  }
-  GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
-
-  if (!is_connection_management (queue->type))
-  {
-    peer->queue_n--;
-  }
-
-  if (NULL != queue->cont)
-  {
-    struct GNUNET_TIME_Relative wait_time;
-
-    wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
-         GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
-    connection_destroyed = queue->cont (queue->cont_cls,
-                                        queue->c, sent, queue->type, pid,
-                                        queue->fwd, queue->size, wait_time);
-  }
-  else
-  {
-    connection_destroyed = GNUNET_NO;
-  }
-
-  if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
-  {
-    GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
-    peer->core_transmit = NULL;
-    peer->tmt_time.abs_value_us = 0;
-  }
-
-  GNUNET_free (queue);
-  GCC_check_connections ();
-  return connection_destroyed;
-}
-
-
-/**
- * @brief Queue and pass message to core when possible.
- *
- * @param peer Peer towards which to queue the message.
- * @param cls Closure (@c type dependant). It will be used by queue_send to
- *            build the message to be sent if not already prebuilt.
- * @param type Type of the message.
- * @param payload_type Type of the message's payload
- *                     0 if the message is a retransmission (unknown payload).
- *                     UINT16_MAX if the message does not have payload.
- * @param payload_id ID of the payload (MID, ACK #, etc)
- * @param size Size of the message.
- * @param c Connection this message belongs to (can be NULL).
- * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
- * @param cont Continuation to be called once CORE has taken the message.
- * @param cont_cls Closure for @c cont.
- *
- * @return Handle to cancel the message before it is sent. Once cont is called
- *         message has been sent and therefore the handle is no longer valid.
- */
-struct CadetPeerQueue *
-GCP_queue_add (struct CadetPeer *peer,
-               void *cls,
-               uint16_t type,
-               uint16_t payload_type,
-               uint32_t payload_id,
-               size_t size,
-               struct CadetConnection *c,
-               int fwd,
-               GCP_sent cont,
-               void *cont_cls)
-{
-  struct CadetPeerQueue *q;
-  int priority;
-  int call_core;
-
-  GCC_check_connections ();
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
-       GC_m2s (type), GC_m2s (payload_type), payload_id,
-       GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
-
-  if (NULL == peer->connections)
-  {
-    /* We are not connected to this peer, ignore request. */
-    LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
-    GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
-                              GNUNET_NO);
-    return NULL;
-  }
-
-  priority = 0;
-  if (is_connection_management (type))
-  {
-    priority = 100;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
-
-  call_core = (NULL == c || GNUNET_MESSAGE_TYPE_CADET_KX == type) ?
-               GNUNET_YES : GCC_is_sendable (c, fwd);
-  q = GNUNET_new (struct CadetPeerQueue);
-  q->cls = cls;
-  q->type = type;
-  q->payload_type = payload_type;
-  q->payload_id = payload_id;
-  q->size = size;
-  q->peer = peer;
-  q->c = c;
-  q->fwd = fwd;
-  q->cont = cont;
-  q->cont_cls = cont_cls;
-  if (100 > priority)
-  {
-    GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
-    peer->queue_n++;
-  }
-  else
-  {
-    GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
-    call_core = GNUNET_YES;
-  }
-
-  q->start_waiting = GNUNET_TIME_absolute_get ();
-  if (NULL == peer->core_transmit && GNUNET_YES == call_core)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "calling core tmt rdy towards %s for %u bytes\n",
-         GCP_2s (peer), size);
-    peer->core_transmit =
-        GNUNET_CORE_notify_transmit_ready (core_handle,
-                                           GNUNET_NO, get_priority (q),
-                                           GNUNET_TIME_UNIT_FOREVER_REL,
-                                           GNUNET_PEER_resolve2 (peer->id),
-                                           get_core_size (size),
-                                           &queue_send, peer);
-    peer->tmt_time = GNUNET_TIME_absolute_get ();
-  }
-  else if (GNUNET_NO == call_core)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
-         GCP_2s (peer));
-
-  }
-  else
-  {
-    struct GNUNET_TIME_Relative elapsed;
-    elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
-         GCP_2s (peer),
-         GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
-
-  }
-  queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
-  GCC_check_connections ();
-  return q;
+    GNUNET_CONTAINER_DLL_remove (q->peer->q_head, q->peer->q_tail, q);
 }
 
 
 /**
- * Cancel all queued messages to a peer that belong to a certain connection.
+ * Function called by MQ when a message is sent to CORE.
  *
- * @param peer Peer towards whom to cancel.
- * @param c Connection whose queued messages to cancel. Might be destroyed by
- *          the sent continuation call.
+ * @param cls Closure (queue handle).
  */
-void
-GCP_queue_cancel (struct CadetPeer *peer,
-                  struct CadetConnection *c)
+static void
+mq_sent (void *cls)
 {
-  struct CadetPeerQueue *q;
-  struct CadetPeerQueue *next;
-  struct CadetPeerQueue *prev;
-  int connection_destroyed;
+    struct CadetPeerQueue *q = cls;
 
-  GCC_check_connections ();
-  connection_destroyed = GNUNET_NO;
-  for (q = peer->queue_head; NULL != q; q = next)
-  {
-    prev = q->prev;
-    if (q->c == c)
+    if (GNUNET_NO == q->management_traffic)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "GMP queue cancel %s\n",
-           GC_m2s (q->type));
-      GNUNET_assert (GNUNET_NO == connection_destroyed);
-      if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
-      {
-        q->c = NULL;
-      }
-      else
-      {
-        connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
-      }
-
-      /* Get next from prev, q->next might be already freed:
-       * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
-       */
-      if (NULL == prev)
-        next = peer->queue_head;
-      else
-        next = prev->next;
+        q->peer->queue_n--;
     }
-    else
-    {
-      next = q->next;
-    }
-  }
-
-  if ( (NULL == peer->queue_head) &&
-       (NULL != peer->core_transmit) )
-  {
-    GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
-    peer->core_transmit = NULL;
-    peer->tmt_time.abs_value_us = 0;
-  }
-  GCC_check_connections ();
+    call_peer_cont (q, GNUNET_YES);
+    GNUNET_free (q);
 }
 
 
 /**
- * Get the first transmittable message for a connection.
+ * Finish the drop operation.
  *
- * @param peer Neighboring peer.
- * @param c Connection.
- *
- * @return First transmittable message.
+ * @param cls queue entry to finish drop for
  */
-static struct CadetPeerQueue *
-connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
+static void
+drop_cb (void *cls)
 {
-  struct CadetPeerQueue *q;
+  struct CadetPeerQueue *q = cls;
 
-  for (q = peer->queue_head; NULL != q; q = q->next)
-  {
-    if (q->c != c)
-      continue;
-    if (queue_is_sendable (q))
-    {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  sendable!!\n");
-      return q;
-    }
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  not sendable\n");
-  }
-
-  return NULL;
+  GNUNET_MQ_discard (q->env);
+  call_peer_cont (q, GNUNET_YES);
+  GNUNET_free (q);
 }
 
 
 /**
- * Get the first message for a connection and unqueue it.
- *
- * Only tunnel (or higher) level messages are unqueued. Connection specific
- * messages are silently destroyed upon encounter.
+ * @brief Send a message to another peer (using CORE).
  *
- * @param peer Neighboring peer.
- * @param c Connection.
- * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
- *                          Can NOT be NULL.
+ * @param peer Peer towards which to queue the message.
+ * @param message Message to send.
+ * @param payload_type Type of the message's payload, for debug messages.
+ *                     0 if the message is a retransmission (unknown payload).
+ *                     UINT16_MAX if the message does not have payload.
+ * @param payload_id ID of the payload (MID, ACK #, etc)
+ * @param c Connection this message belongs to (can be NULL).
+ * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
+ * @param cont Continuation to be called once CORE has sent the message.
+ * @param cont_cls Closure for @c cont.
  *
- * @return First message for this connection.
+ * @return A handle to the message in the queue or NULL (if dropped).
  */
-struct GNUNET_MessageHeader *
-GCP_connection_pop (struct CadetPeer *peer,
-                    struct CadetConnection *c,
-                    int *destroyed)
-{
-  struct CadetPeerQueue *q;
-  struct CadetPeerQueue *next;
-  struct GNUNET_MessageHeader *msg;
-  int dest;
+struct CadetPeerQueue *
+GCP_send (struct CadetPeer *peer,
+          const struct GNUNET_MessageHeader *message,
+          uint16_t payload_type,
+          struct CadetEncryptedMessageIdentifier payload_id,
+          struct CadetConnection *c,
+          int fwd,
+          GCP_sent cont,
+          void *cont_cls)
+{
+    struct CadetPeerQueue *q;
+    uint16_t type;
+    uint16_t size;
 
-  GCC_check_connections ();
-  GNUNET_assert (NULL != destroyed);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on conn %p\n", c);
-  for (q = peer->queue_head; NULL != q; q = next)
-  {
-    next = q->next;
-    if (q->c != c)
-      continue;
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
-         GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
-         q->cont);
-    switch (q->type)
+    GCC_check_connections ();
+    type = ntohs (message->type);
+    size = ntohs (message->size);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
+         GC_m2s (type), GC_m2s (payload_type),
+         ntohl (payload_id.pid),
+         GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
+
+    if (NULL == peer->connections)
     {
-      case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
-      case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
-      case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
-      case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
-      case GNUNET_MESSAGE_TYPE_CADET_ACK:
-      case GNUNET_MESSAGE_TYPE_CADET_POLL:
-        dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
-        if (GNUNET_YES == dest)
-        {
-          GNUNET_break (GNUNET_NO == *destroyed);
-          *destroyed = GNUNET_YES;
-        }
-        continue;
-
-      case GNUNET_MESSAGE_TYPE_CADET_KX:
-      case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
-      case GNUNET_MESSAGE_TYPE_CADET_AX:
-      case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
-        msg = (struct GNUNET_MessageHeader *) q->cls;
-        dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
-        if (GNUNET_YES == dest)
+        /* We are not connected to this peer, ignore request. */
+        GNUNET_break (0);
+        LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
+        GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
+                                  GNUNET_NO);
+        return NULL;
+    }
+
+    q = GNUNET_new (struct CadetPeerQueue);
+    q->env = GNUNET_MQ_msg_copy (message);
+    q->peer = peer;
+    q->cont = cont;
+    q->cont_cls = cont_cls;
+    q->queue_timestamp = GNUNET_TIME_absolute_get ();
+    q->management_traffic = is_connection_management (type);
+    q->type = type;
+    q->size = size;
+    q->payload_type = payload_type;
+    q->payload_id = payload_id;
+    q->c = c;
+    q->c_fwd = fwd;
+    GNUNET_MQ_notify_sent (q->env, &mq_sent, q);
+    GNUNET_CONTAINER_DLL_insert (peer->q_head, peer->q_tail, q);
+
+    if (GNUNET_YES == q->management_traffic)
+    {
+        GNUNET_MQ_send (peer->core_mq, q->env);  // FIXME implement "_urgent", use
+    }
+    else
+    {
+        if (GNUNET_YES == should_I_drop ())
         {
-          GNUNET_break (GNUNET_NO == *destroyed);
-          *destroyed = GNUNET_YES;
+            LOG (GNUNET_ERROR_TYPE_WARNING,
+                "DD %s (%s %u) on conn %s %s (random drop for testing)\n",
+                 GC_m2s (q->type),
+                 GC_m2s (q->payload_type),
+                 ntohl (q->payload_id.pid),
+                 GCC_2s (c),
+                 GC_f2s (q->c_fwd));
+           q->drop_task = GNUNET_SCHEDULER_add_now (&drop_cb,
+                                                    q);
+           return q;
         }
-        return msg;
-
-      default:
-        GNUNET_break (0);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
+        GNUNET_MQ_send (peer->core_mq, q->env);
+        peer->queue_n++;
     }
-  }
-  GCC_check_connections ();
-  return NULL;
+
+    GCC_check_connections ();
+    return q;
 }
 
 
 /**
- * Unlock a possibly locked queue for a connection.
+ * Cancel sending a message. Message must have been sent with
+ * #GCP_send before.  May not be called after the notify sent
+ * callback has been called.
  *
- * If there is a message that can be sent on this connection, call core for it.
- * Otherwise (if core transmit is already called or there is no sendable
- * message) do nothing.
+ * It DOES call the continuation given to #GCP_send.
  *
- * @param peer Peer who keeps the queue.
- * @param c Connection whose messages to unlock.
+ * @param q Queue handle to cancel
  */
 void
-GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
+GCP_send_cancel (struct CadetPeerQueue *q)
 {
-  struct CadetPeerQueue *q;
-  size_t size;
-
-  GCC_check_connections ();
-  if (NULL != peer->core_transmit)
+  if (NULL != q->drop_task)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  already unlocked!\n");
-    return; /* Already unlocked */
+    GNUNET_SCHEDULER_cancel (q->drop_task);
+    q->drop_task = NULL;
+    GNUNET_MQ_discard (q->env);
   }
-
-  q = connection_get_first_message (peer, c);
-  if (NULL == q)
+  else
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  queue empty!\n");
-    return; /* Nothing to transmit */
+    GNUNET_MQ_send_cancel (q->env);
   }
-
-  size = q->size;
-  peer->core_transmit =
-      GNUNET_CORE_notify_transmit_ready (core_handle,
-                                         GNUNET_NO, get_priority (q),
-                                         GNUNET_TIME_UNIT_FOREVER_REL,
-                                         GNUNET_PEER_resolve2 (peer->id),
-                                         get_core_size (size),
-                                         &queue_send,
-                                         peer);
-  peer->tmt_time = GNUNET_TIME_absolute_get ();
-  GCC_check_connections ();
+  call_peer_cont (q, GNUNET_NO);
+  GNUNET_free (q);
 }
 
 
@@ -1800,69 +1283,41 @@ GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
 void
 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "GCP_init\n");
-  in_shutdown = GNUNET_NO;
-  peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
-                                             &max_peers))
-  {
-    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
-                               "CADET", "MAX_PEERS", "USING DEFAULT");
-    max_peers = 1000;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
-                                             &drop_percent))
-  {
-    drop_percent = 0;
-  }
-  else
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
-    LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
-    LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
-    LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
-    LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
-  }
-  ats_ch = GNUNET_ATS_connectivity_init (c);
-  core_handle = GNUNET_CORE_connect (c, /* Main configuration */
-                                     NULL,      /* Closure passed to CADET 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 (GNUNET_YES !=
-      GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
-  {
-    transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
-                                                 /* Notify callbacks */
-                                                 NULL, NULL, NULL);
-  }
-  else
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
-    LOG (GNUNET_ERROR_TYPE_WARNING, "*  DISABLE TRYING CONNECT in config  *\n");
-    LOG (GNUNET_ERROR_TYPE_WARNING, "*  Use this only for test purposes.  *\n");
-    LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
-    transport_handle = NULL;
-  }
-
-
-
-  if (NULL == core_handle)
-  {
-    GNUNET_break (0);
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
+    cfg = c;
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "GCP_init\n");
+    in_shutdown = GNUNET_NO;
+    peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
+    if (GNUNET_OK !=
+            GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
+                    &max_peers))
+    {
+        GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
+                                   "CADET", "MAX_PEERS", "USING DEFAULT");
+        max_peers = 1000;
+    }
 
+    if (GNUNET_OK !=
+            GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
+                    &drop_percent))
+    {
+        drop_percent = 0;
+    }
+    else
+    {
+        LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
+        LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
+        LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
+        LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
+        LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
+    }
+    ats_ch = GNUNET_ATS_connectivity_init (c);
+    connect_to_core (c);
+    if (NULL == core_handle)
+    {
+        GNUNET_break (0);
+        GNUNET_SCHEDULER_shutdown ();
+    }
 }
 
 
@@ -1872,30 +1327,28 @@ GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
 void
 GCP_shutdown (void)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Shutting down peer subsystem\n");
-  in_shutdown = GNUNET_YES;
-  GNUNET_CONTAINER_multipeermap_iterate (peers,
-                                         &shutdown_peer,
-                                         NULL);
-  if (NULL != core_handle)
-  {
-    GNUNET_CORE_disconnect (core_handle);
-    core_handle = NULL;
-  }
-  if (NULL != transport_handle)
-  {
-    GNUNET_TRANSPORT_disconnect (transport_handle);
-    transport_handle = NULL;
-  }
-  if (NULL != ats_ch)
-  {
-    GNUNET_ATS_connectivity_done (ats_ch);
-    ats_ch = NULL;
-  }
-  GNUNET_PEER_change_rc (myid, -1);
-  GNUNET_CONTAINER_multipeermap_destroy (peers);
-  peers = NULL;
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Shutting down peer subsystem\n");
+    in_shutdown = GNUNET_YES;
+    if (NULL != core_handle)
+    {
+        GNUNET_CORE_disconnect (core_handle);
+        core_handle = NULL;
+    }
+    GNUNET_PEER_change_rc (myid, -1);
+    /* With MQ API, CORE calls the disconnect handler for every peer
+     * after calling GNUNET_CORE_disconnect, shutdown must occur *after* that.
+     */
+    GNUNET_CONTAINER_multipeermap_iterate (peers,
+                                           &shutdown_peer,
+                                           NULL);
+    if (NULL != ats_ch)
+    {
+        GNUNET_ATS_connectivity_done (ats_ch);
+        ats_ch = NULL;
+    }
+    GNUNET_CONTAINER_multipeermap_destroy (peers);
+    peers = NULL;
 }
 
 
@@ -1913,26 +1366,26 @@ GCP_shutdown (void)
 struct CadetPeer *
 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
 {
-  struct CadetPeer *peer;
+    struct CadetPeer *peer;
 
-  peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
-  if (NULL == peer)
-  {
-    peer = GNUNET_new (struct CadetPeer);
-    if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
+    peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
+    if (NULL == peer)
     {
-      peer_delete_oldest ();
+        peer = GNUNET_new (struct CadetPeer);
+        if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
+        {
+            peer_delete_oldest ();
+        }
+        GNUNET_assert (GNUNET_OK ==
+                       GNUNET_CONTAINER_multipeermap_put (peers,
+                               peer_id,
+                               peer,
+                               GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+        peer->id = GNUNET_PEER_intern (peer_id);
     }
-    GNUNET_assert (GNUNET_OK ==
-                   GNUNET_CONTAINER_multipeermap_put (peers,
-                                                      peer_id,
-                                                      peer,
-                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-    peer->id = GNUNET_PEER_intern (peer_id);
-  }
-  peer->last_contact = GNUNET_TIME_absolute_get ();
+    peer->last_contact = GNUNET_TIME_absolute_get ();
 
-  return peer;
+    return peer;
 }
 
 
@@ -1951,7 +1404,7 @@ GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
 struct CadetPeer *
 GCP_get_short (const GNUNET_PEER_Id peer, int create)
 {
-  return GCP_get (GNUNET_PEER_resolve2 (peer), create);
+    return GCP_get (GNUNET_PEER_resolve2 (peer), create);
 }
 
 
@@ -1964,9 +1417,9 @@ GCP_get_short (const GNUNET_PEER_Id peer, int create)
 static void
 hello_offer_done (void *cls)
 {
-  struct CadetPeer *peer = cls;
+    struct CadetPeer *peer = cls;
 
-  peer->hello_offer = NULL;
+    peer->hello_offer = NULL;
 }
 
 
@@ -1980,82 +1433,82 @@ hello_offer_done (void *cls)
 void
 GCP_connect (struct CadetPeer *peer)
 {
-  struct CadetTunnel *t;
-  struct CadetPeerPath *path;
-  struct CadetConnection *c;
-  int rerun_search;
+    struct CadetTunnel *t;
+    struct CadetPeerPath *path;
+    struct CadetConnection *c;
+    int rerun_search;
 
-  GCC_check_connections ();
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "peer_connect towards %s\n",
-       GCP_2s (peer));
-  /* If we have a current hello, try to connect using it. */
-  GCP_try_connect (peer);
+    GCC_check_connections ();
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "peer_connect towards %s\n",
+         GCP_2s (peer));
+    /* If we have a current hello, try to connect using it. */
+    GCP_try_connect (peer);
 
-  t = peer->tunnel;
-  c = NULL;
-  rerun_search = GNUNET_NO;
+    t = peer->tunnel;
+    c = NULL;
+    rerun_search = GNUNET_NO;
 
-  if (NULL != peer->path_head)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  some path exists\n");
-    path = peer_get_best_path (peer);
-    if (NULL != path)
+    if (NULL != peer->path_head)
     {
-      char *s;
-
-      s = path_2s (path);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  path to use: %s\n", s);
-      GNUNET_free (s);
-
-      c = GCT_use_path (t, path);
-      if (NULL == c)
-      {
-        /* This case can happen when the path includes a first hop that is
-         * not yet known to be connected.
-         *
-         * This happens quite often during testing when running cadet
-         * under valgrind: core connect notifications come very late
-         * and the DHT result has already come and created a valid
-         * path.  In this case, the peer->connections
-         * hashmaps will be NULL and tunnel_use_path will not be able
-         * to create a connection from that path.
-         *
-         * Re-running the DHT GET should give core time to callback.
-         *
-         * GCT_use_path -> GCC_new -> register_neighbors takes care of
-         * updating statistics about this issue.
-         */
-        rerun_search = GNUNET_YES;
-      }
-      else
-      {
-        GCC_send_create (c);
-        return;
-      }
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "  some path exists\n");
+        path = peer_get_best_path (peer);
+        if (NULL != path)
+        {
+            char *s;
+
+            s = path_2s (path);
+            LOG (GNUNET_ERROR_TYPE_DEBUG, "  path to use: %s\n", s);
+            GNUNET_free (s);
+
+            c = GCT_use_path (t, path);
+            if (NULL == c)
+            {
+                /* This case can happen when the path includes a first hop that is
+                 * not yet known to be connected.
+                 *
+                 * This happens quite often during testing when running cadet
+                 * under valgrind: core connect notifications come very late
+                 * and the DHT result has already come and created a valid
+                 * path.  In this case, the peer->connections
+                 * hashmaps will be NULL and tunnel_use_path will not be able
+                 * to create a connection from that path.
+                 *
+                 * Re-running the DHT GET should give core time to callback.
+                 *
+                 * GCT_use_path -> GCC_new -> register_neighbors takes care of
+                 * updating statistics about this issue.
+                 */
+                rerun_search = GNUNET_YES;
+            }
+            else
+            {
+                GCC_send_create (c);
+                return;
+            }
+        }
+        else
+        {
+            LOG (GNUNET_ERROR_TYPE_DEBUG, "  but is NULL, all paths are in use\n");
+        }
     }
-    else
+
+    if (GNUNET_YES == rerun_search)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  but is NULL, all paths are in use\n");
-    }
-  }
+        struct GNUNET_TIME_Relative delay;
 
-  if (GNUNET_YES == rerun_search)
-  {
-    struct GNUNET_TIME_Relative delay;
+        GCP_stop_search (peer);
+        delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
+        peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
+                               &delayed_search,
+                               peer);
+        GCC_check_connections ();
+        return;
+    }
 
-    GCP_stop_search (peer);
-    delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
-    peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
-                                                         &delayed_search,
-                                                         peer);
+    if (GNUNET_NO == is_searching (peer))
+        GCP_start_search (peer);
     GCC_check_connections ();
-    return;
-  }
-
-  if (GNUNET_NO == is_searching (peer))
-    GCP_start_search (peer);
-  GCC_check_connections ();
 }
 
 
@@ -2069,20 +1522,19 @@ GCP_connect (struct CadetPeer *peer)
 int
 GCP_is_neighbor (const struct CadetPeer *peer)
 {
-  struct CadetPeerPath *path;
+    struct CadetPeerPath *path;
 
-  if (NULL == peer->connections)
-    return GNUNET_NO;
+    if (NULL == peer->connections)
+        return GNUNET_NO;
 
-  for (path = peer->path_head; NULL != path; path = path->next)
-  {
-    if (3 > path->length)
-      return GNUNET_YES;
-  }
+    for (path = peer->path_head; NULL != path; path = path->next)
+    {
+        if (3 > path->length)
+            return GNUNET_YES;
+    }
 
-  /* Is not a neighbor but connections is not NULL, probably disconnecting */
-  GNUNET_break (0);
-  return GNUNET_NO;
+    /* Is not a neighbor but connections is not NULL, probably disconnecting */
+    return GNUNET_NO;
 }
 
 
@@ -2097,11 +1549,11 @@ GCP_is_neighbor (const struct CadetPeer *peer)
 void
 GCP_add_tunnel (struct CadetPeer *peer)
 {
-  GCC_check_connections ();
-  if (NULL != peer->tunnel)
-    return;
-  peer->tunnel = GCT_new (peer);
-  GCC_check_connections ();
+    GCC_check_connections ();
+    if (NULL != peer->tunnel)
+        return;
+    peer->tunnel = GCT_new (peer);
+    GCC_check_connections ();
 }
 
 
@@ -2121,22 +1573,22 @@ GCP_add_connection (struct CadetPeer *peer,
                     struct CadetConnection *c,
                     int pred)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "adding connection %s\n",
-       GCC_2s (c));
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "to peer %s\n",
-       GCP_2s (peer));
-  GNUNET_assert (NULL != peer->connections);
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONTAINER_multihashmap_put (peer->connections,
-                                                    GCC_get_h (c),
-                                                    c,
-                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Peer %s has now %u connections.\n",
-       GCP_2s (peer),
-       GNUNET_CONTAINER_multihashmap_size (peer->connections));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "adding connection %s\n",
+         GCC_2s (c));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "to peer %s\n",
+         GCP_2s (peer));
+    GNUNET_assert (NULL != peer->connections);
+    GNUNET_assert (GNUNET_OK ==
+                   GNUNET_CONTAINER_multishortmap_put (peer->connections,
+                                                       &GCC_get_id (c)->connection_of_tunnel,
+                                                       c,
+                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Peer %s has now %u connections.\n",
+         GCP_2s (peer),
+         GNUNET_CONTAINER_multishortmap_size (peer->connections));
 }
 
 
@@ -2157,90 +1609,90 @@ GCP_add_path (struct CadetPeer *peer,
               struct CadetPeerPath *path,
               int trusted)
 {
-  struct CadetPeerPath *aux;
-  unsigned int l;
-  unsigned int l2;
-
-  GCC_check_connections ();
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "adding path [%u] to peer %s\n",
-       path->length, GCP_2s (peer));
+    struct CadetPeerPath *aux;
+    unsigned int l;
+    unsigned int l2;
 
-  if (NULL == peer || NULL == path
-      || path->peers[path->length - 1] != peer->id)
-  {
-    GNUNET_break (0);
-    path_destroy (path);
-    return NULL;
-  }
+    GCC_check_connections ();
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "adding path [%u] to peer %s\n",
+         path->length, GCP_2s (peer));
 
-  for (l = 1; l < path->length; l++)
-  {
-    if (path->peers[l] == myid)
+    if (NULL == peer || NULL == path
+            || path->peers[path->length - 1] != peer->id)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
-      for (l2 = 0; l2 < path->length - l; l2++)
-      {
-        path->peers[l2] = path->peers[l + l2];
-      }
-      path->length -= l;
-      l = 1;
-      path->peers = GNUNET_realloc (path->peers,
-                                    path->length * sizeof (GNUNET_PEER_Id));
+        GNUNET_break (0);
+        path_destroy (path);
+        return NULL;
     }
-  }
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
-
-  if (2 >= path->length && GNUNET_NO == trusted)
-  {
-    /* Only allow CORE to tell us about direct paths */
-    path_destroy (path);
-    return NULL;
-  }
+    for (l = 1; l < path->length; l++)
+    {
+        if (path->peers[l] == myid)
+        {
+            LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
+            for (l2 = 0; l2 < path->length - l; l2++)
+            {
+                path->peers[l2] = path->peers[l + l2];
+            }
+            path->length -= l;
+            l = 1;
+            path->peers = GNUNET_realloc (path->peers,
+                                          path->length * sizeof (GNUNET_PEER_Id));
+        }
+    }
 
-  l = path_get_length (path);
-  if (0 == l)
-  {
-    path_destroy (path);
-    return NULL;
-  }
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
 
-  GNUNET_assert (peer->id == path->peers[path->length - 1]);
-  for (aux = peer->path_head; aux != NULL; aux = aux->next)
-  {
-    l2 = path_get_length (aux);
-    if (l2 > l)
+    if (2 >= path->length && GNUNET_NO == trusted)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  added\n");
-      GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
-                                          peer->path_tail, aux, path);
-      goto finish;
+        /* Only allow CORE to tell us about direct paths */
+        path_destroy (path);
+        return NULL;
     }
-    else
+
+    l = path_get_length (path);
+    if (0 == l)
     {
-      if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
-      {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "  already known\n");
         path_destroy (path);
-        return aux;
-      }
+        return NULL;
     }
-  }
-  GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
-                                   peer->path_tail,
-                                    path);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  added last\n");
+
+    GNUNET_assert (peer->id == path->peers[path->length - 1]);
+    for (aux = peer->path_head; aux != NULL; aux = aux->next)
+    {
+        l2 = path_get_length (aux);
+        if (l2 > l)
+        {
+            LOG (GNUNET_ERROR_TYPE_DEBUG, "  added\n");
+            GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
+                                                peer->path_tail, aux, path);
+            goto finish;
+        }
+        else
+        {
+            if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
+            {
+                LOG (GNUNET_ERROR_TYPE_DEBUG, "  already known\n");
+                path_destroy (path);
+                return aux;
+            }
+        }
+    }
+    GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
+                                      peer->path_tail,
+                                      path);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  added last\n");
 
 finish:
-  if (NULL != peer->tunnel
-      && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
-      && 2 < path->length) /* Direct paths are handled by core_connect */
-  {
-    GCP_connect (peer);
-  }
-  GCC_check_connections ();
-  return path;
+    if (NULL != peer->tunnel
+            && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
+            && 2 < path->length) /* Direct paths are handled by core_connect */
+    {
+        GCP_connect (peer);
+    }
+    GCC_check_connections ();
+    return path;
 }
 
 
@@ -2263,10 +1715,10 @@ GCP_add_path_to_origin (struct CadetPeer *peer,
                         struct CadetPeerPath *path,
                         int trusted)
 {
-  if (NULL == path)
-    return NULL;
-  path_invert (path);
-  return GCP_add_path (peer, path, trusted);
+    if (NULL == path)
+        return NULL;
+    path_invert (path);
+    return GCP_add_path (peer, path, trusted);
 }
 
 
@@ -2279,22 +1731,23 @@ GCP_add_path_to_origin (struct CadetPeer *peer,
 void
 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
 {
-  unsigned int i;
+    unsigned int i;
 
-  /* TODO: invert and add */
-  GCC_check_connections ();
-  for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
-  for (i++; i < p->length; i++)
-  {
-    struct CadetPeer *peer;
-    struct CadetPeerPath *copy;
+    /* TODO: invert and add to origin */
+    /* TODO: replace all "GCP_add_path" with this, make the other one static */
+    GCC_check_connections ();
+    for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
+    for (i++; i < p->length; i++)
+    {
+        struct CadetPeer *peer;
+        struct CadetPeerPath *copy;
 
-    peer = GCP_get_short (p->peers[i], GNUNET_YES);
-    copy = path_duplicate (p);
-    copy->length = i + 1;
-    GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
-  }
-  GCC_check_connections ();
+        peer = GCP_get_short (p->peers[i], GNUNET_YES);
+        copy = path_duplicate (p);
+        copy->length = i + 1;
+        GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
+    }
+    GCC_check_connections ();
 }
 
 
@@ -2306,33 +1759,33 @@ GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
  */
 void
 GCP_remove_path (struct CadetPeer *peer,
-                struct CadetPeerPath *path)
+                 struct CadetPeerPath *path)
 {
-  struct CadetPeerPath *iter;
-  struct CadetPeerPath *next;
+    struct CadetPeerPath *iter;
+    struct CadetPeerPath *next;
 
-  GCC_check_connections ();
-  GNUNET_assert (myid == path->peers[0]);
-  GNUNET_assert (peer->id == path->peers[path->length - 1]);
+    GCC_check_connections ();
+    GNUNET_assert (myid == path->peers[0]);
+    GNUNET_assert (peer->id == path->peers[path->length - 1]);
 
-  LOG (GNUNET_ERROR_TYPE_INFO,
-       "Removing path %p (%u) from %s\n",
-       path, path->length, GCP_2s (peer));
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Removing path %p (%u) from %s\n",
+         path, path->length, GCP_2s (peer));
 
-  for (iter = peer->path_head; NULL != iter; iter = next)
-  {
-    next = iter->next;
-    if (0 == path_cmp (path, iter))
+    for (iter = peer->path_head; NULL != iter; iter = next)
     {
-      GNUNET_CONTAINER_DLL_remove (peer->path_head,
-                                  peer->path_tail,
-                                  iter);
-      if (iter != path)
-        path_destroy (iter);
+        next = iter->next;
+        if (0 == path_cmp (path, iter))
+        {
+            GNUNET_CONTAINER_DLL_remove (peer->path_head,
+                                         peer->path_tail,
+                                         iter);
+            if (iter != path)
+                path_destroy (iter);
+        }
     }
-  }
-  path_destroy (path);
-  GCC_check_connections ();
+    path_destroy (path);
+    GCC_check_connections ();
 }
 
 
@@ -2348,11 +1801,11 @@ GCP_check_connection (const struct CadetPeer *peer,
 {
   GNUNET_assert (NULL != peer);
   GNUNET_assert (NULL != peer->connections);
-    return;
+  return; // ????
   GNUNET_assert (GNUNET_YES ==
-                 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
-                                                               GCC_get_h (c),
-                                                               c));
+                 GNUNET_CONTAINER_multishortmap_contains_value (peer->connections,
+                                                                &GCC_get_id (c)->connection_of_tunnel,
+                                                                c));
 }
 
 
@@ -2366,23 +1819,23 @@ void
 GCP_remove_connection (struct CadetPeer *peer,
                        const struct CadetConnection *c)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Removing connection %s\n",
-       GCC_2s (c));
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "from peer %s\n",
-       GCP_2s (peer));
-  if ( (NULL == peer) ||
-       (NULL == peer->connections) )
-    return;
-  GNUNET_assert (GNUNET_YES ==
-                 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
-                                                       GCC_get_h (c),
-                                                       c));
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Peer %s remains with %u connections.\n",
-       GCP_2s (peer),
-       GNUNET_CONTAINER_multihashmap_size (peer->connections));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Removing connection %s\n",
+         GCC_2s (c));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "from peer %s\n",
+         GCP_2s (peer));
+    if ( (NULL == peer) ||
+            (NULL == peer->connections) )
+        return;
+    GNUNET_assert (GNUNET_YES ==
+                   GNUNET_CONTAINER_multishortmap_remove (peer->connections,
+                                                          &GCC_get_id (c)->connection_of_tunnel,
+                                                          c));
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Peer %s remains with %u connections.\n",
+         GCP_2s (peer),
+         GNUNET_CONTAINER_multishortmap_size (peer->connections));
 }
 
 
@@ -2395,35 +1848,35 @@ GCP_remove_connection (struct CadetPeer *peer,
 void
 GCP_start_search (struct CadetPeer *peer)
 {
-  const struct GNUNET_PeerIdentity *id;
-  struct CadetTunnel *t = peer->tunnel;
+    const struct GNUNET_PeerIdentity *id;
+    struct CadetTunnel *t = peer->tunnel;
 
-  GCC_check_connections ();
- if (NULL != peer->search_h)
-  {
-    GNUNET_break (0);
-    return;
-  }
+    GCC_check_connections ();
   if (NULL != peer->search_h)
+    {
+        GNUNET_break (0);
+        return;
+    }
 
-  if (NULL != peer->search_delayed)
-    GCP_stop_search (peer);
+    if (NULL != peer->search_delayed)
+        GCP_stop_search (peer);
 
-  id = GNUNET_PEER_resolve2 (peer->id);
-  peer->search_h = GCD_search (id, &search_handler, peer);
+    id = GNUNET_PEER_resolve2 (peer->id);
+    peer->search_h = GCD_search (id, &search_handler, peer);
 
-  if (NULL == t)
-  {
-    /* Why would we search for a peer with no tunnel towards it? */
-    GNUNET_break (0);
-    return;
-  }
+    if (NULL == t)
+    {
+        /* Why would we search for a peer with no tunnel towards it? */
+        GNUNET_break (0);
+        return;
+    }
 
-  if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
-      || 0 == GCT_count_any_connections (t))
-  {
-    GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
-  }
-  GCC_check_connections ();
+    if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
+            || 0 == GCT_count_any_connections (t))
+    {
+        GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
+    }
+    GCC_check_connections ();
 }
 
 
@@ -2436,18 +1889,18 @@ GCP_start_search (struct CadetPeer *peer)
 void
 GCP_stop_search (struct CadetPeer *peer)
 {
-  GCC_check_connections ();
-  if (NULL != peer->search_h)
-  {
-    GCD_search_stop (peer->search_h);
-    peer->search_h = NULL;
-  }
-  if (NULL != peer->search_delayed)
-  {
-    GNUNET_SCHEDULER_cancel (peer->search_delayed);
-    peer->search_delayed = NULL;
-  }
-  GCC_check_connections ();
+    GCC_check_connections ();
+    if (NULL != peer->search_h)
+    {
+        GCD_search_stop (peer->search_h);
+        peer->search_h = NULL;
+    }
+    if (NULL != peer->search_delayed)
+    {
+        GNUNET_SCHEDULER_cancel (peer->search_delayed);
+        peer->search_delayed = NULL;
+    }
+    GCC_check_connections ();
 }
 
 
@@ -2461,7 +1914,7 @@ GCP_stop_search (struct CadetPeer *peer)
 const struct GNUNET_PeerIdentity *
 GCP_get_id (const struct CadetPeer *peer)
 {
-  return GNUNET_PEER_resolve2 (peer->id);
+    return GNUNET_PEER_resolve2 (peer->id);
 }
 
 
@@ -2475,7 +1928,7 @@ GCP_get_id (const struct CadetPeer *peer)
 GNUNET_PEER_Id
 GCP_get_short_id (const struct CadetPeer *peer)
 {
-  return peer->id;
+    return peer->id;
 }
 
 
@@ -2490,11 +1943,11 @@ GCP_get_short_id (const struct CadetPeer *peer)
 void
 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
 {
-  peer->tunnel = t;
-  if (NULL == t && GNUNET_YES == is_searching (peer))
-  {
-    GCP_stop_search (peer);
-  }
+    peer->tunnel = t;
+    if (NULL == t && GNUNET_YES == is_searching (peer))
+    {
+        GCP_stop_search (peer);
+    }
 }
 
 
@@ -2508,9 +1961,9 @@ GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
 struct CadetTunnel *
 GCP_get_tunnel (const struct CadetPeer *peer)
 {
-  if (NULL == peer)
-    return NULL;
-  return peer->tunnel;
+    if (NULL == peer)
+        return NULL;
+    return peer->tunnel;
 }
 
 
@@ -2522,29 +1975,29 @@ GCP_get_tunnel (const struct CadetPeer *peer)
  */
 void
 GCP_set_hello (struct CadetPeer *peer,
-              const struct GNUNET_HELLO_Message *hello)
+               const struct GNUNET_HELLO_Message *hello)
 {
-  struct GNUNET_HELLO_Message *old;
-  size_t size;
+    struct GNUNET_HELLO_Message *old;
+    size_t size;
 
-  GCC_check_connections ();
- LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
-  if (NULL == hello)
-    return;
+    GCC_check_connections ();
   LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
+    if (NULL == hello)
+        return;
 
-  old = GCP_get_hello (peer);
-  if (NULL == old)
-  {
-    size = GNUNET_HELLO_size (hello);
-    peer->hello = GNUNET_malloc (size);
-    memcpy (peer->hello, hello, size);
-  }
-  else
-  {
-    peer->hello = GNUNET_HELLO_merge (old, hello);
-    GNUNET_free (old);
-  }
-  GCC_check_connections ();
+    old = GCP_get_hello (peer);
+    if (NULL == old)
+    {
+        size = GNUNET_HELLO_size (hello);
+        peer->hello = GNUNET_malloc (size);
+        GNUNET_memcpy (peer->hello, hello, size);
+    }
+    else
+    {
+        peer->hello = GNUNET_HELLO_merge (old, hello);
+        GNUNET_free (old);
+    }
+    GCC_check_connections ();
 }
 
 
@@ -2558,22 +2011,22 @@ GCP_set_hello (struct CadetPeer *peer,
 struct GNUNET_HELLO_Message *
 GCP_get_hello (struct CadetPeer *peer)
 {
-  struct GNUNET_TIME_Absolute expiration;
-  struct GNUNET_TIME_Relative remaining;
+    struct GNUNET_TIME_Absolute expiration;
+    struct GNUNET_TIME_Relative remaining;
 
-  if (NULL == peer->hello)
-    return NULL;
+    if (NULL == peer->hello)
+        return NULL;
 
-  expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
-  remaining = GNUNET_TIME_absolute_get_remaining (expiration);
-  if (0 == remaining.rel_value_us)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
-         GNUNET_STRINGS_absolute_time_to_string (expiration));
-    GNUNET_free (peer->hello);
-    peer->hello = NULL;
-  }
-  return peer->hello;
+    expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
+    remaining = GNUNET_TIME_absolute_get_remaining (expiration);
+    if (0 == remaining.rel_value_us)
+    {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
+             GNUNET_STRINGS_absolute_time_to_string (expiration));
+        GNUNET_free (peer->hello);
+        peer->hello = NULL;
+    }
+    return peer->hello;
 }
 
 
@@ -2585,34 +2038,37 @@ GCP_get_hello (struct CadetPeer *peer)
 void
 GCP_try_connect (struct CadetPeer *peer)
 {
-  struct GNUNET_HELLO_Message *hello;
-  struct GNUNET_MessageHeader *mh;
+    struct GNUNET_HELLO_Message *hello;
+    struct GNUNET_MessageHeader *mh;
 
-  if (NULL == transport_handle)
-    return;
-  GCC_check_connections ();
-  if (GNUNET_YES == GCP_is_neighbor (peer))
-    return;
-  hello = GCP_get_hello (peer);
-  if (NULL == hello)
-    return;
+    if (GNUNET_YES !=
+            GNUNET_CONFIGURATION_get_value_yesno (cfg,
+                    "CADET",
+                    "DISABLE_TRY_CONNECT"))
+        return;
+    GCC_check_connections ();
+    if (GNUNET_YES == GCP_is_neighbor (peer))
+        return;
+    hello = GCP_get_hello (peer);
+    if (NULL == hello)
+        return;
 
-  mh = GNUNET_HELLO_get_header (hello);
-  if (NULL != peer->hello_offer)
-  {
-    GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
-    peer->hello_offer = NULL;
-  }
-  peer->hello_offer = GNUNET_TRANSPORT_offer_hello (transport_handle,
-                                                    mh,
-                                                    &hello_offer_done,
-                                                    peer);
-  if (NULL == peer->connectivity_suggestion)
-    peer->connectivity_suggestion
-      = GNUNET_ATS_connectivity_suggest (ats_ch,
-                                         GNUNET_PEER_resolve2 (peer->id),
-                                         1 /* strength */);
-  GCC_check_connections ();
+    mh = GNUNET_HELLO_get_header (hello);
+    if (NULL != peer->hello_offer)
+    {
+        GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
+        peer->hello_offer = NULL;
+    }
+    peer->hello_offer = GNUNET_TRANSPORT_offer_hello (cfg,
+                        mh,
+                        &hello_offer_done,
+                        peer);
+    if (NULL == peer->connectivity_suggestion)
+        peer->connectivity_suggestion
+            = GNUNET_ATS_connectivity_suggest (ats_ch,
+                                               GCP_get_id (peer),
+                                               1);  /* strength */
+    GCC_check_connections ();
 }
 
 
@@ -2629,42 +2085,42 @@ GCP_notify_broken_link (struct CadetPeer *peer,
                         const struct GNUNET_PeerIdentity *peer1,
                         const struct GNUNET_PeerIdentity *peer2)
 {
-  struct CadetPeerPath *iter;
-  struct CadetPeerPath *next;
-  unsigned int i;
-  GNUNET_PEER_Id p1;
-  GNUNET_PEER_Id p2;
+    struct CadetPeerPath *iter;
+    struct CadetPeerPath *next;
+    unsigned int i;
+    GNUNET_PEER_Id p1;
+    GNUNET_PEER_Id p2;
 
-  GCC_check_connections ();
-  p1 = GNUNET_PEER_search (peer1);
-  p2 = GNUNET_PEER_search (peer2);
+    GCC_check_connections ();
+    p1 = GNUNET_PEER_search (peer1);
+    p2 = GNUNET_PEER_search (peer2);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
-  if (0 == p1 || 0 == p2)
-  {
-    /* We don't even know them */
-    return;
-  }
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
+    if (0 == p1 || 0 == p2)
+    {
+        /* We don't even know them */
+        return;
+    }
 
-  for (iter = peer->path_head; NULL != iter; iter = next)
-  {
-    next = iter->next;
-    for (i = 0; i < iter->length - 1; i++)
+    for (iter = peer->path_head; NULL != iter; iter = next)
     {
-      if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
-          || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
-      {
-        char *s;
+        next = iter->next;
+        for (i = 0; i < iter->length - 1; i++)
+        {
+            if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
+                    || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
+            {
+                char *s;
 
-        s = path_2s (iter);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
-        GNUNET_free (s);
+                s = path_2s (iter);
+                LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
+                GNUNET_free (s);
 
-        path_invalidate (iter);
-      }
+                path_invalidate (iter);
+            }
+        }
     }
-  }
-  GCC_check_connections ();
+    GCC_check_connections ();
 }
 
 
@@ -2678,13 +2134,13 @@ GCP_notify_broken_link (struct CadetPeer *peer,
 unsigned int
 GCP_count_paths (const struct CadetPeer *peer)
 {
-  struct CadetPeerPath *iter;
-  unsigned int i;
+    struct CadetPeerPath *iter;
+    unsigned int i;
 
-  for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
-    i++;
+    for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
+        i++;
 
-  return i;
+    return i;
 }
 
 
@@ -2702,17 +2158,17 @@ GCP_iterate_paths (struct CadetPeer *peer,
                    GCP_path_iterator callback,
                    void *cls)
 {
-  struct CadetPeerPath *iter;
-  unsigned int i;
+    struct CadetPeerPath *iter;
+    unsigned int i;
 
-  for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
-  {
-    i++;
-    if (GNUNET_YES != callback (cls, peer, iter))
-      break;
-  }
+    for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
+    {
+        i++;
+        if (GNUNET_YES != callback (cls, peer, iter))
+            break;
+    }
 
-  return i;
+    return i;
 }
 
 
@@ -2726,11 +2182,11 @@ void
 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
                  void *cls)
 {
-  GCC_check_connections ();
-  GNUNET_CONTAINER_multipeermap_iterate (peers,
-                                         iter,
-                                         cls);
-  GCC_check_connections ();
+    GCC_check_connections ();
+    GNUNET_CONTAINER_multipeermap_iterate (peers,
+                                           iter,
+                                           cls);
+    GCC_check_connections ();
 }
 
 
@@ -2744,9 +2200,9 @@ GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
 const char *
 GCP_2s (const struct CadetPeer *peer)
 {
-  if (NULL == peer)
-    return "(NULL)";
-  return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
+    if (NULL == peer)
+        return "(NULL)";
+    return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
 }