src: for every AGPL3.0 file, add SPDX identifier.
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_paths.c
index 7b90fe152d4a6f3c4ddca47e9748d0271ff6bd96..a061c5685b840c7afa3ea0b998b26d49e345c56e 100644 (file)
@@ -2,24 +2,24 @@
      This file is part of GNUnet.
      Copyright (C) 2001-2017 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file cadet/gnunet-service-cadet_paths.c
- * @brief Information we track per path.
+ * @brief Information we track per path.    
  * @author Bartlomiej Polot
  * @author Christian Grothoff
  */
@@ -146,7 +146,7 @@ GCPP_add_connection (struct CadetPeerPath *path,
   struct CadetPeerPathEntry *entry;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Adding connection %s to path %s at offset %u\n",
+       "Adding %s to path %s at offset %u\n",
        GCC_2s (cc),
        GCPP_2s (path),
        off);
@@ -179,7 +179,7 @@ GCPP_del_connection (struct CadetPeerPath *path,
        GCC_2s (cc),
        GCPP_2s (path),
        off);
-  GNUNET_assert (off < path->entries_length); /* FIXME: #4909: This assertion fails sometimes! */
+  GNUNET_assert (off < path->entries_length);
   entry = path->entries[off];
   GNUNET_assert (cc == entry->cc);
   entry->cc = NULL;
@@ -187,33 +187,51 @@ GCPP_del_connection (struct CadetPeerPath *path,
 
 
 /**
- * This path is no longer needed, free resources.
+ * Tries to attach @a path to a peer, working backwards from the end
+ * and stopping at @a stop_at. If path->hn is NULL on return then the
+ * path was not attached and you can assume that path->entries_length
+ * is equal to @a stop_at.
  *
- * @param path path resources to free
+ * @param path the path to attach
+ * @param stop_at the path length at which to stop trying
  */
 static void
-path_destroy (struct CadetPeerPath *path)
+attach_path (struct CadetPeerPath *path, unsigned int stop_at)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Destroying path %s\n",
-       GCPP_2s (path));
-  for (unsigned int i=0;i<path->entries_length;i++)
+  GNUNET_assert (NULL == path->hn);
+
+  /* Try to attach this path to a peer, working backwards from the end. */
+  while (path->entries_length > stop_at)
   {
-    struct CadetPeerPathEntry *entry = path->entries[i];
+    unsigned int end = path->entries_length - 1;
+    struct CadetPeerPathEntry *entry = path->entries[end];
+    int force = GNUNET_NO;
 
+    recalculate_path_desirability (path);
+    /* If the entry already has a connection using it, force attach. */
     if (NULL != entry->cc)
-    {
-      struct CadetTConnection *ct;
+      force = GNUNET_YES;
+    path->hn = GCP_attach_path (entry->peer,
+                                path,
+                                end,
+                                force);
+    if (NULL != path->hn)
+      break;
 
-      ct = GCC_get_ct (entry->cc);
-      if (NULL != ct)
-        GCT_connection_lost (ct);
-      GCC_destroy_without_tunnel (entry->cc);
-    }
+    /* Attach failed, trim this entry from the path. */
+    GNUNET_assert (NULL == entry->cc);
+    GCP_path_entry_remove (entry->peer,
+                           entry,
+                           end);
     GNUNET_free (entry);
+    path->entries[end] = NULL;
+    path->entries_length--;
   }
-  GNUNET_free (path->entries);
-  GNUNET_free (path);
+
+  /* Shrink array to actual path length. */
+  GNUNET_array_grow (path->entries,
+                     path->entries_length,
+                     path->entries_length);
 }
 
 
@@ -228,7 +246,6 @@ void
 GCPP_release (struct CadetPeerPath *path)
 {
   struct CadetPeerPathEntry *entry;
-  int force;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Owner releases path %s\n",
@@ -236,34 +253,23 @@ GCPP_release (struct CadetPeerPath *path)
   path->hn = NULL;
   entry = path->entries[path->entries_length - 1];
   GNUNET_assert (path == entry->path);
-  while (1)
+  GNUNET_assert (NULL == entry->cc);
+  /* cut 'off' end of path */
+  GCP_path_entry_remove (entry->peer,
+                         entry,
+                         path->entries_length - 1);
+  GNUNET_free (entry);
+  path->entries[path->entries_length - 1] = NULL;
+  path->entries_length--;
+  /* see if new peer at the end likes this path any better */
+  attach_path (path, 0);
+  if (NULL == path->hn)
   {
-    /* cut 'off' end of path */
-    GNUNET_assert (NULL == entry->cc);
-    GCP_path_entry_remove (entry->peer,
-                           entry,
-                           path->entries_length - 1);
-    path->entries_length--; /* We don't bother shrinking the 'entries' array,
-                               as it's probably not worth it. */
-    GNUNET_free (entry);
-    if (0 == path->entries_length)
-      break; /* the end */
-
-    /* see if new peer at the end likes this path any better */
-    entry = path->entries[path->entries_length - 1];
-    GNUNET_assert (path == entry->path);
-    force = (NULL == entry->cc) ? GNUNET_NO : GNUNET_YES;
-    path->hn = GCP_attach_path (entry->peer,
-                                path,
-                                path->entries_length - 1,
-                                force);
-    if (NULL != path->hn)
-      return; /* yep, got attached, we are done. */
-    GNUNET_assert (GNUNET_NO == force);
+    /* nobody wants us, discard the path */
+    GNUNET_assert (0 == path->entries_length);
+    GNUNET_assert (NULL == path->entries);
+    GNUNET_free (path);
   }
-
-  /* nobody wants us, discard the path */
-  path_destroy (path);
 }
 
 
@@ -350,7 +356,7 @@ check_match (void *cls,
        (off + 1 != cm_ctx->cpath_length) )
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "check_match missmatch because path %s is too long (%u vs. %u vs. %u)\n",
+         "check_match mismatch because path %s is too long (%u vs. %u vs. %u)\n",
          GCPP_2s (path),
          path->entries_length,
          off + 1,
@@ -363,10 +369,10 @@ check_match (void *cls,
                                  i))
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "check_match path %s missmatches at offset %u\n",
+           "check_match path %s mismatches at offset %u\n",
            GCPP_2s (path),
            i);
-      return GNUNET_YES; /* missmatch, ignore */
+      return GNUNET_YES; /* mismatch, ignore */
     }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "check_match found match with path %s\n",
@@ -422,33 +428,23 @@ extend_path (struct CadetPeerPath *path,
                    path,
                    path->hn);
   path->hn = NULL;
-  for (i=num_peers-1;i>=0;i--)
+  path->entries_length = old_len + num_peers;
+  if (GNUNET_YES == force)
   {
-    struct CadetPeerPathEntry *entry = path->entries[old_len + i];
+    int end = path->entries_length - 1;
 
-    path->entries_length = old_len + i + 1;
-    recalculate_path_desirability (path);
-    if (NULL != entry->cc)
-      force = GNUNET_YES;
-    path->hn = GCP_attach_path (peers[i],
+    path->hn = GCP_attach_path (path->entries[end]->peer,
                                 path,
-                                old_len + (unsigned int) i,
-                                force);
-    if (NULL != path->hn)
-      break;
-    GCP_path_entry_remove (entry->peer,
-                           entry,
-                           old_len + i);
-    GNUNET_free (entry);
-    path->entries[old_len + i] = NULL;
+                                end,
+                                GNUNET_YES);
+  } else {
+    attach_path (path, old_len);
   }
   if (NULL == path->hn)
   {
     /* none of the peers is interested in this path;
-       shrink path back and re-attach. */
-    GNUNET_array_grow (path->entries,
-                       path->entries_length,
-                       old_len);
+       re-attach. */
+    GNUNET_assert (old_len == path->entries_length);
     path->hn = GCP_attach_path (path->entries[old_len - 1]->peer,
                                 path,
                                 old_len - 1,
@@ -483,8 +479,6 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
   struct CadetPeer *cpath[get_path_length + put_path_length];
   struct CheckMatchContext cm_ctx;
   struct CadetPeerPath *path;
-  struct GNUNET_CONTAINER_HeapNode *hn;
-  int i;
   unsigned int skip;
   unsigned int total_len;
 
@@ -501,6 +495,14 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
     pid = (off < get_path_length)
       ? &get_path[get_path_length - off - 1]
       : &put_path[get_path_length + put_path_length - off - 1];
+    /* Check that I am not in the path */
+    if (0 == memcmp (&my_full_id,
+                     pid,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+      skip = off + 1;
+      continue;
+    }
     cpath[off - skip] = GCP_get (pid,
                                  GNUNET_YES);
     /* Check that no peer is twice on the path */
@@ -513,6 +515,12 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
       }
     }
   }
+  if (skip >= total_len)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Path discovered from DHT is one big cycle?\n");
+    return;
+  }
   total_len -= skip;
 
   /* First figure out if this path is a subset of an existing path, an
@@ -520,7 +528,7 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
   cm_ctx.cpath_length = total_len;
   cm_ctx.cpath = cpath;
   cm_ctx.match = NULL;
-  for (i=total_len-1;i>=0;i--)
+  for (int i=total_len-1;i>=0;i--)
   {
     GCP_iterate_paths_at (cpath[i],
                           (unsigned int) i,
@@ -555,7 +563,7 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
   path->entries_length = total_len;
   path->entries = GNUNET_new_array (path->entries_length,
                                     struct CadetPeerPathEntry *);
-  for (i=path->entries_length-1;i>=0;i--)
+  for (int i=path->entries_length-1;i>=0;i--)
   {
     struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
 
@@ -563,7 +571,7 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
     entry->peer = cpath[i];
     entry->path = path;
   }
-  for (i=path->entries_length-1;i>=0;i--)
+  for (int i=path->entries_length-1;i>=0;i--)
   {
     struct CadetPeerPathEntry *entry = path->entries[i];
 
@@ -573,39 +581,17 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
   }
 
   /* Finally, try to attach it */
-  hn = NULL;
-  for (i=total_len-1;i>=0;i--)
-  {
-    struct CadetPeerPathEntry *entry = path->entries[i];
-
-    path->entries_length = i + 1;
-    recalculate_path_desirability (path);
-    hn = GCP_attach_path (cpath[i],
-                          path,
-                          (unsigned int) i,
-                          GNUNET_NO);
-    if (NULL != hn)
-      break;
-    GCP_path_entry_remove (entry->peer,
-                           entry,
-                           i);
-    GNUNET_free (entry);
-    path->entries[i] = NULL;
-  }
-  if (NULL == hn)
+  attach_path (path, 0);
+  if (NULL == path->hn)
   {
     /* None of the peers on the path care about it. */
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Path discovered from DHT is not interesting to us\n");
-    GNUNET_free (path->entries);
+    GNUNET_assert (0 == path->entries_length);
+    GNUNET_assert (NULL == path->entries);
     GNUNET_free (path);
     return;
   }
-  path->hn = hn;
-  /* Shrink path to actual useful length */
-  GNUNET_array_grow (path->entries,
-                     path->entries_length,
-                     i + 1);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Created new path %s based on information from DHT\n",
        GCPP_2s (path));