pass only CadetTunnelAxolotl if it suffices, preparation for having ambiguous KX...
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet-new_core.c
index 7cff68f4abfe5e9c53fb569fb86a81f730ae2548..086337c9ab25af579fa1d776744470e503fa459d 100644 (file)
@@ -312,6 +312,8 @@ send_broken (struct RouteDirection *target,
   struct GNUNET_MQ_Envelope *env;
   struct GNUNET_CADET_ConnectionBrokenMessage *bm;
 
+  if (NULL == target->mqm)
+    return; /* Can't send notification, connection is down! */
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Notifying %s about BROKEN route at %s-%s of connection %s\n",
        GCP_2s (target->hop),
@@ -326,6 +328,7 @@ send_broken (struct RouteDirection *target,
     bm->peer1 = *peer1;
   if (NULL != peer2)
     bm->peer2 = *peer2;
+
   GCP_request_mq_cancel (target->mqm,
                          env);
   target->mqm = NULL;
@@ -443,7 +446,7 @@ handle_connection_create (void *cls,
   {
     /* Duplicate CREATE, pass it on, previous one might have been lost! */
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Passing on duplicate CREATE message on connection %s\n",
+         "Passing on duplicate CADET_CONNECTION_CREATE message on connection %s\n",
          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
     route_message (sender,
                    &msg->cid,
@@ -462,7 +465,7 @@ handle_connection_create (void *cls,
     if (NULL != cc)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "Received duplicate CREATE message on connection %s\n",
+           "Received duplicate CADET_CONNECTION_CREATE message on connection %s\n",
            GNUNET_sh2s (&msg->cid.connection_of_tunnel));
       GCC_handle_duplicate_create (cc);
       return;
@@ -471,14 +474,35 @@ handle_connection_create (void *cls,
     origin = GCP_get (&pids[0],
                       GNUNET_YES);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Received CREATE message from %s for connection %s, building inverse path\n",
+         "Received CADET_CONNECTION_CREATE message from %s for connection %s, building inverse path\n",
          GCP_2s (origin),
          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
     path = GCPP_get_path_from_route (path_length - 1,
                                      pids);
-    GCT_add_inbound_connection (GCT_create_tunnel (origin),
-                                &msg->cid,
-                                path);
+    if (GNUNET_OK !=
+        GCT_add_inbound_connection (GCP_get_tunnel (origin,
+                                                    GNUNET_YES),
+                                    &msg->cid,
+                                    path))
+    {
+      /* Send back BROKEN: duplicate connection on the same path,
+         we will use the other one. */
+      struct GNUNET_MQ_Envelope *env;
+      struct GNUNET_CADET_ConnectionBrokenMessage *bm;
+
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Received CADET_CONNECTION_CREATE from %s for %s, but %s already has a connection. Sending BROKEN\n",
+           GCP_2s (sender),
+           GNUNET_sh2s (&msg->cid.connection_of_tunnel),
+           GCPP_2s (path));
+      env = GNUNET_MQ_msg (bm,
+                           GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
+      bm->cid = msg->cid;
+      bm->peer1 = my_full_id;
+      GCP_send_ooo (sender,
+                    env);
+      return;
+    }
     return;
   }
   /* We are merely a hop on the way, check if we can support the route */
@@ -492,7 +516,7 @@ handle_connection_create (void *cls,
     struct GNUNET_CADET_ConnectionBrokenMessage *bm;
 
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
+         "Received CADET_CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
          GCP_2s (sender),
          GNUNET_sh2s (&msg->cid.connection_of_tunnel),
          GNUNET_i2s (&pids[off + 1]),
@@ -509,7 +533,7 @@ handle_connection_create (void *cls,
 
   /* Workable route, create routing entry */
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n",
+       "Received CADET_CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n",
        GCP_2s (sender),
        GNUNET_sh2s (&msg->cid.connection_of_tunnel),
        GNUNET_i2s (&pids[off + 1]),
@@ -607,7 +631,7 @@ handle_connection_broken (void *cls,
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Received CONNECTION_BROKEN for connection %s. Destroying it.\n",
          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
-    GCC_destroy (cc);
+    GCC_destroy_without_core (cc);
 
     /* FIXME: also destroy the path up to the specified link! */
     return;
@@ -657,7 +681,7 @@ handle_connection_destroy (void *cls,
          "Received CONNECTION_DESTROY for connection %s. Destroying connection.\n",
          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
 
-    GCC_destroy (cc);
+    GCC_destroy_without_core (cc);
     return;
   }