tighten formatting rules
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_paths.c
index 79eed0dccdc4d65190bd916dc209653606748836..087515a5ec0945f8fc2920b8cf1b8cc24b2f6547 100644 (file)
@@ -2,21 +2,21 @@
      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 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.
-*/
+     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/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
+ */
 /**
  * @file cadet/gnunet-service-cadet_paths.c
  * @brief Information we track per path.
@@ -30,7 +30,7 @@
 #include "gnunet-service-cadet_paths.h"
 
 
-#define LOG(level, ...) GNUNET_log_from(level,"cadet-pat",__VA_ARGS__)
+#define LOG(level, ...) GNUNET_log_from (level, "cadet-pat", __VA_ARGS__)
 
 
 /**
@@ -38,7 +38,6 @@
  */
 struct CadetPeerPath
 {
-
   /**
    * Array of all the peers on the path.  If @e hn is non-NULL, the
    * last one is our owner.
@@ -61,7 +60,6 @@ struct CadetPeerPath
    * Length of the @e entries array.
    */
   unsigned int entries_length;
-
 };
 
 
@@ -75,7 +73,7 @@ recalculate_path_desirability (struct CadetPeerPath *path)
 {
   double result = 0.0;
 
-  for (unsigned int i=0;i<path->entries_length;i++)
+  for (unsigned int i = 0; i < path->entries_length; i++)
   {
     struct CadetPeer *cp = path->entries[i]->peer;
 
@@ -146,7 +144,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);
@@ -158,7 +156,6 @@ GCPP_add_connection (struct CadetPeerPath *path,
 }
 
 
-
 /**
  * Notify @a path that it is no longer used for connection @a cc which
  * ended at the path's offset @a off.
@@ -179,7 +176,7 @@ GCPP_del_connection (struct CadetPeerPath *path,
        GCC_2s (cc),
        GCPP_2s (path),
        off);
-  GNUNET_assert (off < path->entries_length); /* FIXME: This assertion fails sometimes! */
+  GNUNET_assert (off < path->entries_length);
   entry = path->entries[off];
   GNUNET_assert (cc == entry->cc);
   entry->cc = NULL;
@@ -187,33 +184,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 +243,6 @@ void
 GCPP_release (struct CadetPeerPath *path)
 {
   struct CadetPeerPathEntry *entry;
-  int force;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Owner releases path %s\n",
@@ -236,34 +250,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);
 }
 
 
@@ -309,7 +312,6 @@ GCPP_update_score (struct CadetPeerPath *path,
  */
 struct CheckMatchContext
 {
-
   /**
    * Set to a matching path, if any.
    */
@@ -324,7 +326,6 @@ struct CheckMatchContext
    * How long is the @e cpath array?
    */
   unsigned int cpath_length;
-
 };
 
 
@@ -346,27 +347,27 @@ check_match (void *cls,
   struct CheckMatchContext *cm_ctx = cls;
 
   GNUNET_assert (path->entries_length > off);
-  if ( (path->entries_length != off + 1) &&
-       (off + 1 != cm_ctx->cpath_length) )
+  if ((path->entries_length != off + 1) &&
+      (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,
          cm_ctx->cpath_length);
-    return GNUNET_YES; /* too long, goes somewhere else already, thus cannot be useful */
+    return GNUNET_YES;   /* too long, goes somewhere else already, thus cannot be useful */
   }
-  for (unsigned int i=0;i<off;i++)
+  for (unsigned int i = 0; i < off; i++)
     if (cm_ctx->cpath[i] !=
         GCPP_get_peer_at_offset (path,
                                  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",
@@ -399,7 +400,7 @@ extend_path (struct CadetPeerPath *path,
   GNUNET_array_grow (path->entries,
                      path->entries_length,
                      old_len + num_peers);
-  for (i=num_peers-1;i >= 0;i--)
+  for (i = num_peers - 1; i >= 0; i--)
   {
     struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
 
@@ -407,7 +408,7 @@ extend_path (struct CadetPeerPath *path,
     entry->peer = peers[i];
     entry->path = path;
   }
-  for (i=num_peers-1;i >= 0;i--)
+  for (i = num_peers - 1; i >= 0; i--)
   {
     struct CadetPeerPathEntry *entry = path->entries[old_len + i];
 
@@ -418,36 +419,29 @@ extend_path (struct CadetPeerPath *path,
 
   /* If we extend an existing path, detach it from the
      old owner and re-attach to the new one */
-  GCP_detach_path (path->entries[old_len-1]->peer,
+  GCP_detach_path (path->entries[old_len - 1]->peer,
                    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);
-    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;
-    GNUNET_assert (NULL == entry->cc);
-    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,
@@ -482,8 +476,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;
 
@@ -491,27 +483,40 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
   skip = 0;
   memset (cpath,
           0,
-          sizeof (cpath)); /* Just to trigger harder errors later. */
+          sizeof(cpath));  /* Just to trigger harder errors later. */
   total_len = get_path_length + put_path_length;
-  for (unsigned int off=0;off<total_len;off++)
+  for (unsigned int off = 0; off < total_len; off++)
   {
     const struct GNUNET_PeerIdentity *pid;
 
     pid = (off < get_path_length)
-      ? &get_path[get_path_length - off - 1]
-      : &put_path[get_path_length + put_path_length - off - 1];
+          ? &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 == GNUNET_memcmp (&my_full_id,
+                            pid))
+    {
+      skip = off + 1;
+      continue;
+    }
     cpath[off - skip] = GCP_get (pid,
                                  GNUNET_YES);
     /* Check that no peer is twice on the path */
-    for (unsigned int i=0;i<off - skip;i++)
+    for (unsigned int i = 0; i < off - skip; i++)
     {
-     if (cpath[i] == cpath[off - skip])
+      if (cpath[i] == cpath[off - skip])
       {
         skip = off - i;
         break;
       }
     }
   }
+  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
@@ -519,7 +524,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,
@@ -554,7 +559,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);
 
@@ -562,7 +567,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];
 
@@ -572,39 +577,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));
@@ -628,7 +611,7 @@ GCPP_get_path_from_route (unsigned int path_length,
 
   /* precompute inverted 'cpath' so we can avoid doing the lookups and
      have the correct order */
-  for (unsigned int off=0;off<path_length;off++)
+  for (unsigned int off = 0; off < path_length; off++)
     cpath[off] = GCP_get (&pids[path_length - 1 - off],
                           GNUNET_YES);
 
@@ -637,7 +620,7 @@ GCPP_get_path_from_route (unsigned int path_length,
   cm_ctx.cpath = cpath;
   cm_ctx.cpath_length = path_length;
   cm_ctx.match = NULL;
-  for (int i=path_length-1;i>=0;i--)
+  for (int i = path_length - 1; i >= 0; i--)
   {
     GCP_iterate_paths_at (cpath[i],
                           (unsigned int) i,
@@ -677,7 +660,7 @@ GCPP_get_path_from_route (unsigned int path_length,
   path->entries_length = path_length;
   path->entries = GNUNET_new_array (path->entries_length,
                                     struct CadetPeerPathEntry *);
-  for (int i=path_length-1;i>=0;i--)
+  for (int i = path_length - 1; i >= 0; i--)
   {
     struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
 
@@ -685,7 +668,7 @@ GCPP_get_path_from_route (unsigned int path_length,
     entry->peer = cpath[i];
     entry->path = path;
   }
-  for (int i=path_length-1;i>=0;i--)
+  for (int i = path_length - 1; i >= 0; i--)
   {
     struct CadetPeerPathEntry *entry = path->entries[i];
 
@@ -774,24 +757,25 @@ GCPP_2s (struct CadetPeerPath *path)
        i < path->entries_length;
        i++)
   {
-    if ( (path->entries_length > max_plen) &&
-         (i == max_plen / 2) )
+    if ((path->entries_length > max_plen) &&
+        (i == max_plen / 2))
       off += GNUNET_snprintf (&buf[off],
-                              sizeof (buf) - off,
+                              sizeof(buf) - off,
                               "...-");
-    if ( (path->entries_length > max_plen) &&
-         (i > max_plen / 2) &&
-         (i < path->entries_length - max_plen / 2) )
+    if ((path->entries_length > max_plen) &&
+        (i > max_plen / 2) &&
+        (i < path->entries_length - max_plen / 2))
       continue;
     off += GNUNET_snprintf (&buf[off],
-                            sizeof (buf) - off,
+                            sizeof(buf) - off,
                             "%s%s",
-                            GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path,
-                                                                             i))),
-                            (i == path->entries_length -1) ? "" : "-");
+                            GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (
+                                                      path,
+                                                      i))),
+                            (i == path->entries_length - 1) ? "" : "-");
   }
   GNUNET_snprintf (&buf[off],
-                   sizeof (buf) - off,
+                   sizeof(buf) - off,
                    "(%p)",
                    path);
   return buf;