-
/*
This file is part of GNUnet.
Copyright (C) 2001-2017 GNUnet e.V.
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
-
/**
* @file cadet/gnunet-service-cadet-new_paths.c
* @brief Information we track per path.
* (and will be tricky to have during path changes)
*/
#include "platform.h"
+#include "gnunet-service-cadet-new_connection.h"
#include "gnunet-service-cadet-new_peer.h"
#include "gnunet-service-cadet-new_paths.h"
+#define LOG(level, ...) GNUNET_log_from(level,"cadet-pat",__VA_ARGS__)
+
+
/**
* Information regarding a possible path to reach a peer.
*/
* @param path path to traverse
* @param destination destination node to get to, must be on path
* @param off offset of @a destination on @a path
- * @return NULL if @a create is NO and we have no existing connection
+ * @return NULL if we have no existing connection
* otherwise connection from us to @a destination via @a path
*/
struct CadetConnection *
{
struct CadetPeerPathEntry *entry;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding connection %s to path %s at offset %u\n",
+ GCC_2s (cc),
+ GCPP_2s (path),
+ off);
GNUNET_assert (off < path->entries_length);
entry = &path->entries[off];
GNUNET_assert (NULL == entry->cc);
{
struct CadetPeerPathEntry *entry;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Removing connection %s to path %s at offset %u\n",
+ GCC_2s (cc),
+ GCPP_2s (path),
+ off);
GNUNET_assert (off < path->entries_length);
entry = &path->entries[off];
GNUNET_assert (cc == entry->cc);
static void
path_destroy (struct CadetPeerPath *path)
{
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Destroying path %s\n",
+ GCPP_2s (path));
GNUNET_assert (0 ==
GNUNET_CONTAINER_multipeermap_size (path->connections));
GNUNET_CONTAINER_multipeermap_destroy (path->connections);
{
struct CadetPeerPathEntry *entry;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Owner releases path %s\n",
+ GCPP_2s (path));
path->hn = NULL;
entry = &path->entries[path->entries_length - 1];
while (1)
entry = &path->entries[path->entries_length - 1];
path->hn = GCP_attach_path (entry->peer,
path,
- path->entries_length);
+ path->entries_length,
+ GNUNET_NO);
if (NULL != path->hn)
return; /* yep, got attached, we are done. */
}
* @param path path to extend
* @param peers list of peers beyond the end of @a path
* @param num_peers length of the @a peers array
+ * @param force force attachment, even if we have other
+ * paths already
*/
static void
extend_path (struct CadetPeerPath *path,
struct CadetPeer **peers,
- unsigned int num_peers)
+ unsigned int num_peers,
+ int force)
{
unsigned int old_len = path->entries_length;
struct GNUNET_CONTAINER_HeapNode *hn;
/* FIXME: note that path->desirability is used, but not yet updated here! */
hn = GCP_attach_path (peers[i],
path,
- old_len + (unsigned int) i);
+ old_len + (unsigned int) i,
+ GNUNET_YES);
if (NULL != hn)
break;
}
entry,
old_len + i);
}
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Extended path %s\n",
+ GCPP_2s (path));
}
const struct GNUNET_PeerIdentity *put_path,
unsigned int put_path_length)
{
- struct CheckMatchContext cm_ctx;
struct CadetPeer *cpath[get_path_length + put_path_length];
+ struct CheckMatchContext cm_ctx;
struct CadetPeerPath *path;
struct GNUNET_CONTAINER_HeapNode *hn;
int i;
if (i == get_path_length + put_path_length - 1)
{
/* Existing path includes this one, nothing to do! */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Path discovered from DHT is already known\n");
return;
}
if (cm_ctx.match->entries_length == i + 1)
{
/* Existing path ends in the middle of new path, extend it! */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Trying to extend existing path %s by additional links discovered from DHT\n",
+ GCPP_2s (cm_ctx.match));
extend_path (cm_ctx.match,
&cpath[i],
- get_path_length + put_path_length - i);
+ get_path_length + put_path_length - i,
+ GNUNET_NO);
return;
}
}
/* FIXME: note that path->desirability is used, but not yet initialized here! */
hn = GCP_attach_path (cpath[i],
path,
- (unsigned int) i);
+ (unsigned int) i,
+ GNUNET_NO);
if (NULL != hn)
break;
}
if (NULL == 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);
return;
}
entry,
i);
}
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Created new path %s based on information from DHT\n",
+ GCPP_2s (path));
+}
+
+
+/**
+ * We got an incoming connection, obtain the corresponding path.
+ *
+ * @param path_length number of segments on the @a path
+ * @param pids path through the network, in reverse order (we are at the end at index @a path_length)
+ * @return corresponding path object
+ */
+struct CadetPeerPath *
+GCPP_get_path_from_route (unsigned int path_length,
+ const struct GNUNET_PeerIdentity *pids)
+{
+ struct CheckMatchContext cm_ctx;
+ struct CadetPeer *cpath[path_length];
+ struct CadetPeerPath *path;
+
+ /* precompute inverted 'cpath' so we can avoid doing the lookups and
+ have the correct order */
+ for (unsigned int off=0;off<path_length;off++)
+ cpath[off] = GCP_get (&pids[path_length - 1 - off],
+ GNUNET_YES);
+
+ /* First figure out if this path is a subset of an existing path, an
+ extension of an existing path, or a new path. */
+ cm_ctx.cpath = cpath;
+ cm_ctx.match = NULL;
+ for (int i=path_length-1;i>=0;i--)
+ {
+ GCP_iterate_paths_at (cpath[i],
+ (unsigned int) i,
+ &check_match,
+ &cm_ctx);
+ if (NULL != cm_ctx.match)
+ {
+ if (i == path_length - 1)
+ {
+ /* Existing path includes this one, return the match! */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Returning existing path %s as inverse for incoming connection\n",
+ GCPP_2s (cm_ctx.match));
+ return cm_ctx.match;
+ }
+ if (cm_ctx.match->entries_length == i + 1)
+ {
+ /* Existing path ends in the middle of new path, extend it! */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Extending existing path %s to create inverse for incoming connection\n",
+ GCPP_2s (cm_ctx.match));
+ extend_path (cm_ctx.match,
+ &cpath[i],
+ path_length - i,
+ GNUNET_YES);
+ /* Check that extension was successful */
+ GNUNET_assert (cm_ctx.match->entries_length == path_length);
+ return cm_ctx.match;
+ }
+ }
+ }
+
+ /* No match at all, create completely new path */
+ path = GNUNET_new (struct CadetPeerPath);
+ path->entries_length = path_length;
+ path->entries = GNUNET_new_array (path->entries_length,
+ struct CadetPeerPathEntry);
+ for (int i=path_length-1;i>=0;i--)
+ {
+ struct CadetPeerPathEntry *entry = &path->entries[i];
+
+ entry->peer = cpath[i];
+ entry->path = path;
+ GCP_path_entry_add (entry->peer,
+ entry,
+ i);
+ }
+ path->hn = GCP_attach_path (cpath[path_length - 1],
+ path,
+ path_length - 1,
+ GNUNET_YES);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Created new path %s to create inverse for incoming connection\n",
+ GCPP_2s (path));
+ return path;
}
GCPP_get_peer_at_offset (struct CadetPeerPath *path,
unsigned int off)
{
+ GNUNET_assert (off < path->entries_length);
return path->entries[off].peer;
}
+/**
+ * Convert a path to a human-readable string.
+ *
+ * @param path path to convert
+ * @return string, to be freed by caller (unlike other *_2s APIs!)
+ */
+const char *
+GCPP_2s (struct CadetPeerPath *path)
+{
+ static char buf[2048];
+ size_t off;
+ const unsigned int max_plen = sizeof(buf) / 5 - 2; /* 5 characters per entry */
+
+ off = 0;
+ for (unsigned int i = 0;
+ i < path->entries_length;
+ i++)
+ {
+ if ( (path->entries_length > max_plen) &&
+ (i == max_plen / 2) )
+ off += GNUNET_snprintf (&buf[off],
+ sizeof (buf) - off,
+ "... ");
+ 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,
+ "%s ",
+ GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path,
+ i))));
+ }
+ return buf;
+}
+
+
/* end of gnunet-service-cadet-new_paths.c */