/*
This file is part of GNUnet.
- Copyright (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2009, 2010, 2011, 2015 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
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., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
- * @file dht/gnunet-service-dht_datacache.c
+ * @file dht/gnunet-service-wdht_datacache.c
* @brief GNUnet DHT service's datacache integration
* @author Christian Grothoff
* @author Nathan Evans
#include "gnunet_datacache_lib.h"
#include "gnunet-service-wdht_clients.h"
#include "gnunet-service-wdht_datacache.h"
-#include "gnunet-service-wdht_routing.h"
#include "gnunet-service-wdht_neighbours.h"
#include "gnunet-service-dht.h"
#define DEBUG(...) \
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
+/**
+ * How many "closest" results to we return for migration when
+ * asked (at most)?
+ */
+#define NUM_CLOSEST 42
+
/**
* Handle to the datacache service (for inserting/retrieving data)
*/
*
* @param expiration when will the reply expire
* @param key the query this reply is for
- * @param put_path_length number of peers in 'put_path'
+ * @param put_path_length number of peers in @a put_path
* @param put_path path the reply took on put
+ * @param get_path_length number of peers in @a get_path
+ * @param get_path path the reply took on get
* @param type type of the reply
- * @param data_size number of bytes in 'data'
+ * @param data_size number of bytes in @a data
* @param data application payload data
*/
void
GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
- const struct GNUNET_HashCode * key,
+ const struct GNUNET_HashCode *key,
unsigned int put_path_length,
const struct GNUNET_PeerIdentity *put_path,
- enum GNUNET_BLOCK_Type type, size_t data_size,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *get_path,
+ enum GNUNET_BLOCK_Type type,
+ size_t data_size,
const void *data)
{
int r;
+ struct GNUNET_PeerIdentity path[get_path_length + put_path_length];
if (NULL == datacache)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- _("%s request received, but have no datacache!\n"), "PUT");
+ _("PUT request received, but have no datacache!\n"));
return;
}
if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
GNUNET_break (0);
return;
}
-
+ memcpy (path,
+ put_path,
+ put_path_length * sizeof (struct GNUNET_PeerIdentity));
+ memcpy (&path[put_path_length],
+ get_path,
+ get_path_length * sizeof (struct GNUNET_PeerIdentity));
/* Put size is actual data size plus struct overhead plus path length (if any) */
- GNUNET_STATISTICS_update (GDS_stats,
- gettext_noop ("# ITEMS stored in datacache"), 1,
- GNUNET_NO);
-
- struct GNUNET_PeerIdentity peer = GDS_NEIGHBOURS_get_my_id();
- DEBUG("DATACACHE_PUT KEY = %s, peer = %s\n",GNUNET_h2s(key),GNUNET_i2s(&peer));
- r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
- put_path_length, put_path);
+ r = GNUNET_DATACACHE_put (datacache,
+ key,
+ data_size,
+ data,
+ type,
+ expiration,
+ get_path_length + put_path_length,
+ path);
+ if (GNUNET_OK == r)
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# ITEMS stored in datacache"), 1,
+ GNUNET_NO);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
- GNUNET_h2s (key), data_size, r, put_path_length);
+ GNUNET_h2s (key),
+ data_size,
+ r,
+ put_path_length + get_path_length);
}
-/**
- * List of peers in the get path.
- */
-struct GetPath
-{
- /**
- * Pointer to next item in the list
- */
- struct GetPath *next;
-
- /**
- * Pointer to previous item in the list
- */
- struct GetPath *prev;
-
- /**
- * An element in the get path.
- */
- struct GNUNET_PeerIdentity peer;
-};
-
/**
* Context containing information about a GET request.
struct GNUNET_HashCode key;
/**
- * Number of bytes in xquery.
+ * The trail this request was for
*/
- size_t xquery_size;
+ const struct GNUNET_HashCode *trail_id;
/**
- * Mutator value for the reply_bf, see gnunet_block_lib.h
+ * Number of bytes in @e xquery.
*/
- uint32_t reply_bf_mutator;
+ size_t xquery_size;
/**
- * Total number of peers in get path.
+ * Mutator value for the @e reply_bf, see gnunet_block_lib.h
*/
- unsigned int get_path_length;
+ uint32_t reply_bf_mutator;
/**
* Return value to give back.
enum GNUNET_BLOCK_EvaluationResult eval;
/**
- * Peeer which has the data for the key.
- */
- struct GNUNET_PeerIdentity source_peer;
-
- /**
- * Next hop to forward the get result to.
- */
- struct GNUNET_PeerIdentity next_hop;
-
- /**
- * Head of get path.
+ * Routing options of the GET.
*/
- struct GetPath *head;
+ enum GNUNET_DHT_RouteOption options;
- /**
- * Tail of get path.
- */
- struct GetPath *tail;
-
- /* get_path */
};
gettext_noop
("# Good RESULTS found in datacache"), 1,
GNUNET_NO);
- struct GNUNET_PeerIdentity *get_path;
- get_path = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
- ctx->get_path_length);
- struct GetPath *iterator;
- iterator = ctx->head;
- int i = 0;
- while (i < ctx->get_path_length)
- {
- get_path[i] = iterator->peer;
- i++;
- iterator = iterator->next;
- }
- GDS_NEIGHBOURS_send_get_result (key,type, &(ctx->next_hop),&(ctx->source_peer),
- put_path_length, put_path, ctx->get_path_length,
- get_path, exp, data, size );
- GNUNET_free_non_null (get_path);
-
+ GDS_NEIGHBOURS_send_get_result (ctx->trail_id,
+ ctx->options,
+ key,
+ type,
+ put_path_length,
+ put_path,
+ exp,
+ data,
+ size);
break;
case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
GNUNET_STATISTICS_update (GDS_stats,
/**
* Handle a GET request we've received from another peer.
*
+ * @param trail_id trail identifying where to send the result to, NULL for us
+ * @param options routing options (to be passed along)
* @param key the query
* @param type requested data type
* @param xquery extended query
- * @param xquery_size number of bytes in xquery
+ * @param xquery_size number of bytes in @a xquery
* @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
- * @param reply_bf_mutator mutation value for reply_bf
+ * @param reply_bf_mutator mutation value for @a reply_bf
* @return evaluation result for the local replies
- * @get_path_length Total number of peers in get path
- * @get_path Peers in get path.
*/
enum GNUNET_BLOCK_EvaluationResult
-GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
- enum GNUNET_BLOCK_Type type, const void *xquery,
+GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *trail_id,
+ enum GNUNET_DHT_RouteOption options,
+ const struct GNUNET_HashCode *key,
+ enum GNUNET_BLOCK_Type type,
+ const void *xquery,
size_t xquery_size,
struct GNUNET_CONTAINER_BloomFilter **reply_bf,
- uint32_t reply_bf_mutator,
- uint32_t get_path_length,
- struct GNUNET_PeerIdentity *get_path,
- struct GNUNET_PeerIdentity *next_hop,
- struct GNUNET_PeerIdentity *source_peer)
+ uint32_t reply_bf_mutator)
{
struct GetRequestContext ctx;
unsigned int r;
- if (datacache == NULL)
+ if (NULL == datacache)
return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
GNUNET_STATISTICS_update (GDS_stats,
gettext_noop ("# GET requests given to datacache"),
1, GNUNET_NO);
ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
+ ctx.trail_id = trail_id;
+ ctx.options = options;
ctx.key = *key;
ctx.xquery = xquery;
ctx.xquery_size = xquery_size;
ctx.reply_bf = reply_bf;
ctx.reply_bf_mutator = reply_bf_mutator;
- ctx.get_path_length = get_path_length;
+ r = GNUNET_DATACACHE_get (datacache,
+ key,
+ type,
+ &datacache_get_iterator,
+ &ctx);
+ DEBUG ("DATACACHE_GET for key %s completed (%d). %u results found.\n",
+ GNUNET_h2s (key),
+ ctx.eval,
+ r);
+ return ctx.eval;
+}
- if (next_hop != NULL)
- {
- memcpy (&(ctx.next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
- }
- unsigned int i = 0;
- ctx.head = NULL;
- ctx.tail = NULL;
- if (get_path != NULL)
+/**
+ * Function called with a random element from the datacache.
+ * Stores the key in the closure.
+ *
+ * @param cls a `struct GNUNET_HashCode *`, where to store the @a key
+ * @param key key for the content
+ * @param data_size number of bytes in @a data
+ * @param data content stored
+ * @param type type of the content
+ * @param exp when will the content expire?
+ * @param path_info_len number of entries in @a path_info
+ * @param path_info a path through the network
+ * @return #GNUNET_OK to continue iterating, #GNUNET_SYSERR to abort
+ */
+static int
+datacache_random_iterator (void *cls,
+ const struct GNUNET_HashCode *key,
+ size_t data_size,
+ const char *data,
+ enum GNUNET_BLOCK_Type type,
+ struct GNUNET_TIME_Absolute exp,
+ unsigned int path_info_len,
+ const struct GNUNET_PeerIdentity *path_info)
+{
+ struct GNUNET_HashCode *dest = cls;
+
+ *dest = *key;
+ return GNUNET_OK; /* should actually not matter which we return */
+}
+
+
+/**
+ * Obtain a random key from the datacache.
+ * Used by Whanau for load-balancing.
+ *
+ * @param[out] key where to store the key of a random element,
+ * randomized by PRNG if datacache is empty
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR if the datacache is empty
+ */
+int
+GDS_DATACACHE_get_random_key (struct GNUNET_HashCode *key)
+{
+ if (0 ==
+ GNUNET_DATACACHE_get_random (datacache,
+ &datacache_random_iterator,
+ key))
{
- while (i < get_path_length)
- {
- struct GetPath *element;
- element = GNUNET_new (struct GetPath);
- element->next = NULL;
- element->prev = NULL;
- element->peer = get_path[i];
- GNUNET_CONTAINER_DLL_insert_tail (ctx.head, ctx.tail, element);
- i++;
- }
+ /* randomize key in this case */
+ GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
+ key);
+ return GNUNET_SYSERR;
}
+ return GNUNET_OK;
+}
- r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
- &ctx);
- DEBUG ("DATACACHE_GET for key %s completed (%d). %u results found.\n",GNUNET_h2s (key), ctx.eval, r);
- return ctx.eval;
+
+/**
+ * Iterator for local get request results,
+ *
+ * @param cls closure with the `struct GNUNET_HashCode *` with the trail ID
+ * @param key the key this data is stored under
+ * @param size the size of the data identified by key
+ * @param data the actual data
+ * @param type the type of the data
+ * @param exp when does this value expire?
+ * @param put_path_length number of peers in @a put_path
+ * @param put_path path the reply took on put
+ * @return #GNUNET_OK to continue iteration, anything else
+ * to stop iteration.
+ */
+static int
+datacache_get_successors_iterator (void *cls,
+ const struct GNUNET_HashCode *key,
+ size_t size,
+ const char *data,
+ enum GNUNET_BLOCK_Type type,
+ struct GNUNET_TIME_Absolute exp,
+ unsigned int put_path_length,
+ const struct GNUNET_PeerIdentity *put_path)
+{
+ const struct GNUNET_HashCode *trail_id = cls;
+
+ /* NOTE: The datacache currently does not store the RO from
+ the original 'put', so we don't know the 'correct' option
+ at this point anymore. Thus, we conservatively assume
+ that recording is desired (for now). */
+ GDS_NEIGHBOURS_send_get_result (trail_id,
+ GNUNET_DHT_RO_RECORD_ROUTE,
+ key,
+ type,
+ put_path_length, put_path,
+ exp,
+ data,
+ size);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Handle a request for data close to a key that we have received from
+ * another peer.
+ *
+ * @param trail_id trail where the reply needs to be send to
+ * @param key the location at which the peer is looking for data that is close
+ */
+void
+GDS_DATACACHE_get_successors (const struct GNUNET_HashCode *trail_id,
+ const struct GNUNET_HashCode *key)
+{
+ (void) GNUNET_DATACACHE_get_closest (datacache,
+ key,
+ NUM_CLOSEST,
+ &datacache_get_successors_iterator,
+ (void *) trail_id);
}
void
GDS_DATACACHE_done ()
{
- if (datacache != NULL)
+ if (NULL != datacache)
{
GNUNET_DATACACHE_destroy (datacache);
datacache = NULL;
}
-/* end of gnunet-service-dht_datacache.c */
+/* end of gnunet-service-wdht_datacache.c */