[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r31062 - gnunet/src/dht
From: |
gnunet |
Subject: |
[GNUnet-SVN] r31062 - gnunet/src/dht |
Date: |
Thu, 5 Dec 2013 11:25:19 +0100 |
Author: grothoff
Date: 2013-12-05 11:25:18 +0100 (Thu, 05 Dec 2013)
New Revision: 31062
Added:
gnunet/src/dht/gnunet-service-xdht.c
gnunet/src/dht/gnunet-service-xdht_clients.c
gnunet/src/dht/gnunet-service-xdht_clients.h
gnunet/src/dht/gnunet-service-xdht_datacache.c
gnunet/src/dht/gnunet-service-xdht_datacache.h
gnunet/src/dht/gnunet-service-xdht_hello.c
gnunet/src/dht/gnunet-service-xdht_hello.h
gnunet/src/dht/gnunet-service-xdht_neighbours.c
gnunet/src/dht/gnunet-service-xdht_neighbours.h
gnunet/src/dht/gnunet-service-xdht_nse.c
gnunet/src/dht/gnunet-service-xdht_nse.h
gnunet/src/dht/gnunet-service-xdht_routing.c
gnunet/src/dht/gnunet-service-xdht_routing.h
Modified:
gnunet/src/dht/Makefile.am
gnunet/src/dht/dht.conf.in
Log:
-template for supriti/xvine
Modified: gnunet/src/dht/Makefile.am
===================================================================
--- gnunet/src/dht/Makefile.am 2013-12-05 10:17:09 UTC (rev 31061)
+++ gnunet/src/dht/Makefile.am 2013-12-05 10:25:18 UTC (rev 31062)
@@ -49,7 +49,8 @@
libexec_PROGRAMS = \
- gnunet-service-dht
+ gnunet-service-dht \
+ gnunet-service-dht-xvine
noinst_PROGRAMS = \
gnunet-dht-monitor \
@@ -77,6 +78,29 @@
$(top_builddir)/src/util/libgnunetutil.la \
-lm
+gnunet_service_dht_xvine_SOURCES = \
+ gnunet-service-xdht.c gnunet-service-xdht.h \
+ gnunet-service-xdht_clients.c gnunet-service-xdht_clients.h \
+ gnunet-service-xdht_datacache.c gnunet-service-xdht_datacache.h \
+ gnunet-service-xdht_hello.c gnunet-service-xdht_hello.h \
+ gnunet-service-xdht_nse.c gnunet-service-xdht_nse.h \
+ gnunet-service-xdht_neighbours.c gnunet-service-xdht_neighbours.h \
+ gnunet-service-xdht_routing.c gnunet-service-xdht_routing.h
+gnunet_service_dht_xvine_LDADD = \
+ $(top_builddir)/src/statistics/libgnunetstatistics.la \
+ $(top_builddir)/src/core/libgnunetcore.la \
+ $(top_builddir)/src/nse/libgnunetnse.la \
+ $(top_builddir)/src/ats/libgnunetats.la \
+ $(top_builddir)/src/transport/libgnunettransport.la \
+ $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \
+ $(top_builddir)/src/hello/libgnunethello.la \
+ $(top_builddir)/src/block/libgnunetblock.la \
+ $(top_builddir)/src/datacache/libgnunetdatacache.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ -lm
+
+
+
gnunet_dht_get_SOURCES = \
gnunet-dht-get.c
gnunet_dht_get_LDADD = \
Modified: gnunet/src/dht/dht.conf.in
===================================================================
--- gnunet/src/dht/dht.conf.in 2013-12-05 10:17:09 UTC (rev 31061)
+++ gnunet/src/dht/dht.conf.in 2013-12-05 10:25:18 UTC (rev 31062)
@@ -11,7 +11,7 @@
UNIX_MATCH_UID = YES
UNIX_MATCH_GID = YES
# DISABLE_SOCKET_FORWARDING = NO
-# USERNAME =
+# USERNAME =
# MAXBUF =
# TIMEOUT =
# DISABLEV6 =
Copied: gnunet/src/dht/gnunet-service-xdht.c (from rev 31026,
gnunet/src/dht/gnunet-service-dht.c)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht.c (rev 0)
+++ gnunet/src/dht/gnunet-service-xdht.c 2013-12-05 10:25:18 UTC (rev
31062)
@@ -0,0 +1,199 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht.c
+ * @brief GNUnet DHT service
+ * @author Christian Grothoff
+ * @author Nathan Evans
+ */
+#include "platform.h"
+#include "gnunet_block_lib.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_transport_service.h"
+#include "gnunet_hello_lib.h"
+#include "gnunet_dht_service.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet-service-dht.h"
+#include "gnunet-service-dht_clients.h"
+#include "gnunet-service-dht_datacache.h"
+#include "gnunet-service-dht_hello.h"
+#include "gnunet-service-dht_neighbours.h"
+#include "gnunet-service-dht_nse.h"
+#include "gnunet-service-dht_routing.h"
+
+
+
+/**
+ * Handle for the statistics service.
+ */
+struct GNUNET_STATISTICS_Handle *GDS_stats;
+
+/**
+ * Our handle to the BLOCK library.
+ */
+struct GNUNET_BLOCK_Context *GDS_block_context;
+
+/**
+ * The configuration the DHT service is running with
+ */
+const struct GNUNET_CONFIGURATION_Handle *GDS_cfg;
+
+/**
+ * Our HELLO
+ */
+struct GNUNET_MessageHeader *GDS_my_hello;
+
+/**
+ * Handle to the transport service, for getting our hello
+ */
+struct GNUNET_TRANSPORT_Handle *GDS_transport_handle;
+
+/**
+ * Handle to get our current HELLO.
+ */
+static struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
+
+/**
+ * Hello address expiration
+ */
+struct GNUNET_TIME_Relative hello_expiration;
+
+
+/**
+ * Receive the HELLO from transport service, free current and replace
+ * if necessary.
+ *
+ * @param cls NULL
+ * @param message HELLO message of peer
+ */
+static void
+process_hello (void *cls, const struct GNUNET_MessageHeader *message)
+{
+ GNUNET_assert (message != NULL);
+ GNUNET_free_non_null (GDS_my_hello);
+ GDS_my_hello = GNUNET_malloc (ntohs (message->size));
+ memcpy (GDS_my_hello, message, ntohs (message->size));
+}
+
+
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (NULL != ghh)
+ {
+ GNUNET_TRANSPORT_get_hello_cancel (ghh);
+ ghh = NULL;
+ }
+ if (GDS_transport_handle != NULL)
+ {
+ GNUNET_TRANSPORT_disconnect (GDS_transport_handle);
+ GDS_transport_handle = NULL;
+ }
+ GDS_NEIGHBOURS_done ();
+ GDS_DATACACHE_done ();
+ GDS_ROUTING_done ();
+ GDS_HELLO_done ();
+ GDS_NSE_done ();
+ if (GDS_block_context != NULL)
+ {
+ GNUNET_BLOCK_context_destroy (GDS_block_context);
+ GDS_block_context = NULL;
+ }
+ if (GDS_stats != NULL)
+ {
+ GNUNET_STATISTICS_destroy (GDS_stats, GNUNET_YES);
+ GDS_stats = NULL;
+ }
+ GNUNET_free_non_null (GDS_my_hello);
+ GDS_my_hello = NULL;
+}
+
+
+/**
+ * Process dht requests.
+ *
+ * @param cls closure
+ * @param server the initialized server
+ * @param c configuration to use
+ */
+static void
+run (void *cls, struct GNUNET_SERVER_Handle *server,
+ const struct GNUNET_CONFIGURATION_Handle *c)
+{
+ GDS_cfg = c;
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION",
&hello_expiration))
+ {
+ hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
+ }
+ GDS_block_context = GNUNET_BLOCK_context_create (GDS_cfg);
+ GDS_stats = GNUNET_STATISTICS_create ("dht", GDS_cfg);
+ GDS_ROUTING_init ();
+ GDS_NSE_init ();
+ GDS_DATACACHE_init ();
+ GDS_HELLO_init ();
+ GDS_CLIENTS_init (server);
+ if (GNUNET_OK != GDS_NEIGHBOURS_init ())
+ {
+ shutdown_task (NULL, NULL);
+ return;
+ }
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
+ NULL);
+ GDS_transport_handle =
+ GNUNET_TRANSPORT_connect (GDS_cfg, NULL, NULL, NULL, NULL, NULL);
+ if (GDS_transport_handle == NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Failed to connect to transport service!\n"));
+ return;
+ }
+ ghh = GNUNET_TRANSPORT_get_hello (GDS_transport_handle, &process_hello,
NULL);
+}
+
+
+/**
+ * The main function for the dht service.
+ *
+ * @param argc number of arguments from the command line
+ * @param argv command line arguments
+ * @return 0 ok, 1 on error
+ */
+int
+main (int argc, char *const *argv)
+{
+ int ret;
+
+ ret =
+ (GNUNET_OK ==
+ GNUNET_SERVICE_run (argc, argv, "dht", GNUNET_SERVICE_OPTION_NONE, &run,
+ NULL)) ? 0 : 1;
+ GDS_CLIENTS_done ();
+ return ret;
+}
+
+/* end of gnunet-service-dht.c */
Copied: gnunet/src/dht/gnunet-service-xdht_clients.c (from rev 31026,
gnunet/src/dht/gnunet-service-dht_clients.c)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_clients.c
(rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_clients.c 2013-12-05 10:25:18 UTC
(rev 31062)
@@ -0,0 +1,1514 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_clients.c
+ * @brief GNUnet DHT service's client management code
+ * @author Christian Grothoff
+ * @author Nathan Evans
+ */
+
+#include "platform.h"
+#include "gnunet_constants.h"
+#include "gnunet_protocols.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet-service-dht.h"
+#include "gnunet-service-dht_clients.h"
+#include "gnunet-service-dht_datacache.h"
+#include "gnunet-service-dht_neighbours.h"
+#include "dht.h"
+
+
+/**
+ * Should routing details be logged to stderr (for debugging)?
+ */
+#define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
+
+#define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
+
+/**
+ * Linked list of messages to send to clients.
+ */
+struct PendingMessage
+{
+ /**
+ * Pointer to next item in the list
+ */
+ struct PendingMessage *next;
+
+ /**
+ * Pointer to previous item in the list
+ */
+ struct PendingMessage *prev;
+
+ /**
+ * Actual message to be sent, allocated at the end of the struct:
+ * // msg = (cast) &pm[1];
+ * // memcpy (&pm[1], data, len);
+ */
+ const struct GNUNET_MessageHeader *msg;
+
+};
+
+
+/**
+ * Struct containing information about a client,
+ * handle to connect to it, and any pending messages
+ * that need to be sent to it.
+ */
+struct ClientList
+{
+ /**
+ * Linked list of active clients
+ */
+ struct ClientList *next;
+
+ /**
+ * Linked list of active clients
+ */
+ struct ClientList *prev;
+
+ /**
+ * The handle to this client
+ */
+ struct GNUNET_SERVER_Client *client_handle;
+
+ /**
+ * Handle to the current transmission request, NULL
+ * if none pending.
+ */
+ struct GNUNET_SERVER_TransmitHandle *transmit_handle;
+
+ /**
+ * Linked list of pending messages for this client
+ */
+ struct PendingMessage *pending_head;
+
+ /**
+ * Tail of linked list of pending messages for this client
+ */
+ struct PendingMessage *pending_tail;
+
+};
+
+
+/**
+ * Entry in the DHT routing table for a client's GET request.
+ */
+struct ClientQueryRecord
+{
+
+ /**
+ * The key this request was about
+ */
+ struct GNUNET_HashCode key;
+
+ /**
+ * Client responsible for the request.
+ */
+ struct ClientList *client;
+
+ /**
+ * Extended query (see gnunet_block_lib.h), allocated at the end of this
struct.
+ */
+ const void *xquery;
+
+ /**
+ * Replies we have already seen for this request.
+ */
+ struct GNUNET_HashCode *seen_replies;
+
+ /**
+ * Pointer to this nodes heap location in the retry-heap (for fast removal)
+ */
+ struct GNUNET_CONTAINER_HeapNode *hnode;
+
+ /**
+ * What's the delay between re-try operations that we currently use for this
+ * request?
+ */
+ struct GNUNET_TIME_Relative retry_frequency;
+
+ /**
+ * What's the next time we should re-try this request?
+ */
+ struct GNUNET_TIME_Absolute retry_time;
+
+ /**
+ * The unique identifier of this request
+ */
+ uint64_t unique_id;
+
+ /**
+ * Number of bytes in xquery.
+ */
+ size_t xquery_size;
+
+ /**
+ * Number of entries in 'seen_replies'.
+ */
+ unsigned int seen_replies_count;
+
+ /**
+ * Desired replication level
+ */
+ uint32_t replication;
+
+ /**
+ * Any message options for this request
+ */
+ uint32_t msg_options;
+
+ /**
+ * The type for the data for the GET request.
+ */
+ enum GNUNET_BLOCK_Type type;
+
+};
+
+
+/**
+ * Struct containing paremeters of monitoring requests.
+ */
+struct ClientMonitorRecord
+{
+
+ /**
+ * Next element in DLL.
+ */
+ struct ClientMonitorRecord *next;
+
+ /**
+ * Previous element in DLL.
+ */
+ struct ClientMonitorRecord *prev;
+
+ /**
+ * Type of blocks that are of interest
+ */
+ enum GNUNET_BLOCK_Type type;
+
+ /**
+ * Key of data of interest, NULL for all.
+ */
+ struct GNUNET_HashCode *key;
+
+ /**
+ * Flag whether to notify about GET messages.
+ */
+ int16_t get;
+
+ /**
+ * Flag whether to notify about GET_REPONSE messages.
+ */
+ int16_t get_resp;
+
+ /**
+ * Flag whether to notify about PUT messages.
+ */
+ uint16_t put;
+
+ /**
+ * Client to notify of these requests.
+ */
+ struct ClientList *client;
+};
+
+
+/**
+ * List of active clients.
+ */
+static struct ClientList *client_head;
+
+/**
+ * List of active clients.
+ */
+static struct ClientList *client_tail;
+
+/**
+ * List of active monitoring requests.
+ */
+static struct ClientMonitorRecord *monitor_head;
+
+/**
+ * List of active monitoring requests.
+ */
+static struct ClientMonitorRecord *monitor_tail;
+
+/**
+ * Hashmap for fast key based lookup, maps keys to 'struct ClientQueryRecord'
entries.
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
+
+/**
+ * Heap with all of our client's request, sorted by retry time (earliest on
top).
+ */
+static struct GNUNET_CONTAINER_Heap *retry_heap;
+
+/**
+ * Task that re-transmits requests (using retry_heap).
+ */
+static GNUNET_SCHEDULER_TaskIdentifier retry_task;
+
+
+/**
+ * Task run to check for messages that need to be sent to a client.
+ *
+ * @param client a ClientList, containing the client and any messages to be
sent to it
+ */
+static void
+process_pending_messages (struct ClientList *client);
+
+
+/**
+ * Add a PendingMessage to the clients list of messages to be sent
+ *
+ * @param client the active client to send the message to
+ * @param pending_message the actual message to send
+ */
+static void
+add_pending_message (struct ClientList *client,
+ struct PendingMessage *pending_message)
+{
+ GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
+ pending_message);
+ process_pending_messages (client);
+}
+
+
+/**
+ * Find a client if it exists, add it otherwise.
+ *
+ * @param client the server handle to the client
+ *
+ * @return the client if found, a new client otherwise
+ */
+static struct ClientList *
+find_active_client (struct GNUNET_SERVER_Client *client)
+{
+ struct ClientList *pos = client_head;
+ struct ClientList *ret;
+
+ while (pos != NULL)
+ {
+ if (pos->client_handle == client)
+ return pos;
+ pos = pos->next;
+ }
+ ret = GNUNET_new (struct ClientList);
+ ret->client_handle = client;
+ GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ret);
+ return ret;
+}
+
+
+/**
+ * Iterator over hash map entries that frees all entries
+ * associated with the given client.
+ *
+ * @param cls client to search for in source routes
+ * @param key current key code (ignored)
+ * @param value value in the hash map, a ClientQueryRecord
+ * @return #GNUNET_YES (we should continue to iterate)
+ */
+static int
+remove_client_records (void *cls, const struct GNUNET_HashCode * key, void
*value)
+{
+ struct ClientList *client = cls;
+ struct ClientQueryRecord *record = value;
+
+ if (record->client != client)
+ return GNUNET_YES;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Removing client %p's record for key %s\n", client,
+ GNUNET_h2s (key));
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (forward_map, key,
+ record));
+ if (NULL != record->hnode)
+ GNUNET_CONTAINER_heap_remove_node (record->hnode);
+ GNUNET_array_grow (record->seen_replies, record->seen_replies_count, 0);
+ GNUNET_free (record);
+ return GNUNET_YES;
+}
+
+
+/**
+ * Functions with this signature are called whenever a client
+ * is disconnected on the network level.
+ *
+ * @param cls closure (NULL for dht)
+ * @param client identification of the client; NULL
+ * for the last call when the server is destroyed
+ */
+static void
+handle_client_disconnect (void *cls,
+ struct GNUNET_SERVER_Client *client)
+{
+ struct ClientList *pos;
+ struct PendingMessage *reply;
+ struct ClientMonitorRecord *monitor;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Local client %p disconnects\n",
+ client);
+ pos = find_active_client (client);
+ GNUNET_CONTAINER_DLL_remove (client_head, client_tail, pos);
+ if (pos->transmit_handle != NULL)
+ GNUNET_SERVER_notify_transmit_ready_cancel (pos->transmit_handle);
+ while (NULL != (reply = pos->pending_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, reply);
+ GNUNET_free (reply);
+ }
+ monitor = monitor_head;
+ while (NULL != monitor)
+ {
+ if (monitor->client == pos)
+ {
+ struct ClientMonitorRecord *next;
+
+ GNUNET_free_non_null (monitor->key);
+ next = monitor->next;
+ GNUNET_CONTAINER_DLL_remove (monitor_head, monitor_tail, monitor);
+ GNUNET_free (monitor);
+ monitor = next;
+ }
+ else
+ monitor = monitor->next;
+ }
+ GNUNET_CONTAINER_multihashmap_iterate (forward_map, &remove_client_records,
+ pos);
+ GNUNET_free (pos);
+}
+
+
+/**
+ * Route the given request via the DHT. This includes updating
+ * the bloom filter and retransmission times, building the P2P
+ * message and initiating the routing operation.
+ */
+static void
+transmit_request (struct ClientQueryRecord *cqr)
+{
+ int32_t reply_bf_mutator;
+ struct GNUNET_CONTAINER_BloomFilter *reply_bf;
+ struct GNUNET_CONTAINER_BloomFilter *peer_bf;
+
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# GET requests from clients injected"), 1,
+ GNUNET_NO);
+ reply_bf_mutator =
+ (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+ UINT32_MAX);
+ reply_bf =
+ GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator, cqr->seen_replies,
+ cqr->seen_replies_count);
+ peer_bf =
+ GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Initiating GET for %s, replication %u, already have %u replies\n",
+ GNUNET_h2s(&cqr->key), cqr->replication, cqr->seen_replies_count);
+ GDS_NEIGHBOURS_handle_get (cqr->type, cqr->msg_options, cqr->replication,
+ 0 /* hop count */ ,
+ &cqr->key, cqr->xquery, cqr->xquery_size,
reply_bf,
+ reply_bf_mutator, peer_bf);
+ GNUNET_CONTAINER_bloomfilter_free (reply_bf);
+ GNUNET_CONTAINER_bloomfilter_free (peer_bf);
+
+ /* exponential back-off for retries.
+ * max GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD (15 min) */
+ cqr->retry_frequency = GNUNET_TIME_STD_BACKOFF (cqr->retry_frequency);
+ cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
+}
+
+
+/**
+ * Task that looks at the 'retry_heap' and transmits all of the requests
+ * on the heap that are ready for transmission. Then re-schedules
+ * itself (unless the heap is empty).
+ *
+ * @param cls unused
+ * @param tc scheduler context
+ */
+static void
+transmit_next_request_task (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct ClientQueryRecord *cqr;
+ struct GNUNET_TIME_Relative delay;
+
+ retry_task = GNUNET_SCHEDULER_NO_TASK;
+ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ return;
+ while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
+ {
+ cqr->hnode = NULL;
+ delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
+ if (delay.rel_value_us > 0)
+ {
+ cqr->hnode =
+ GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
+ cqr->retry_time.abs_value_us);
+ retry_task =
+ GNUNET_SCHEDULER_add_delayed (delay, &transmit_next_request_task,
+ NULL);
+ return;
+ }
+ transmit_request (cqr);
+ cqr->hnode =
+ GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
+ cqr->retry_time.abs_value_us);
+ }
+}
+
+
+/**
+ * Handler for PUT messages.
+ *
+ * @param cls closure for the service
+ * @param client the client we received this message from
+ * @param message the actual message received
+ */
+static void
+handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ const struct GNUNET_DHT_ClientPutMessage *dht_msg;
+ struct GNUNET_CONTAINER_BloomFilter *peer_bf;
+ uint16_t size;
+ struct PendingMessage *pm;
+ struct GNUNET_DHT_ClientPutConfirmationMessage *conf;
+
+ size = ntohs (message->size);
+ if (size < sizeof (struct GNUNET_DHT_ClientPutMessage))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# PUT requests received from clients"), 1,
+ GNUNET_NO);
+ dht_msg = (const struct GNUNET_DHT_ClientPutMessage *) message;
+ LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "XDHT CLIENT-PUT %s @ %u\n",
+ GNUNET_h2s (&dht_msg->key), getpid ());
+ /* give to local clients */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Handling local PUT of %u-bytes for query %s\n",
+ size - sizeof (struct GNUNET_DHT_ClientPutMessage),
+ GNUNET_h2s (&dht_msg->key));
+ GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
+ &dht_msg->key, 0, NULL, 0, NULL,
+ ntohl (dht_msg->type),
+ size - sizeof (struct GNUNET_DHT_ClientPutMessage),
+ &dht_msg[1]);
+ /* store locally */
+ GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
+ &dht_msg->key, 0, NULL, ntohl (dht_msg->type),
+ size - sizeof (struct GNUNET_DHT_ClientPutMessage),
+ &dht_msg[1]);
+ /* route to other peers */
+ peer_bf =
+ GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type), ntohl (dht_msg->options),
+ ntohl (dht_msg->desired_replication_level),
+ GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
+ 0 /* hop count */ ,
+ peer_bf, &dht_msg->key, 0, NULL, &dht_msg[1],
+ size -
+ sizeof (struct GNUNET_DHT_ClientPutMessage));
+ GDS_CLIENTS_process_put (ntohl (dht_msg->options),
+ ntohl (dht_msg->type),
+ 0,
+ ntohl (dht_msg->desired_replication_level),
+ 1,
+ GDS_NEIGHBOURS_get_id(),
+ GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
+ &dht_msg->key,
+ &dht_msg[1],
+ size - sizeof (struct GNUNET_DHT_ClientPutMessage));
+ GNUNET_CONTAINER_bloomfilter_free (peer_bf);
+ pm = GNUNET_malloc (sizeof (struct PendingMessage) +
+ sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage));
+ conf = (struct GNUNET_DHT_ClientPutConfirmationMessage *) &pm[1];
+ conf->header.size = htons (sizeof (struct
GNUNET_DHT_ClientPutConfirmationMessage));
+ conf->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK);
+ conf->reserved = htonl (0);
+ conf->unique_id = dht_msg->unique_id;
+ pm->msg = &conf->header;
+ add_pending_message (find_active_client (client), pm);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
+/**
+ * Handler for DHT GET messages from the client.
+ *
+ * @param cls closure for the service
+ * @param client the client we received this message from
+ * @param message the actual message received
+ */
+static void
+handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ const struct GNUNET_DHT_ClientGetMessage *get;
+ struct ClientQueryRecord *cqr;
+ size_t xquery_size;
+ const char *xquery;
+ uint16_t size;
+
+ size = ntohs (message->size);
+ if (size < sizeof (struct GNUNET_DHT_ClientGetMessage))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
+ get = (const struct GNUNET_DHT_ClientGetMessage *) message;
+ xquery = (const char *) &get[1];
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# GET requests received from clients"), 1,
+ GNUNET_NO);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received GET request for %s from local client %p, xq: %.*s\n",
+ GNUNET_h2s (&get->key), client, xquery_size, xquery);
+
+ LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "XDHT CLIENT-GET %s @ %u\n",
+ GNUNET_h2s (&get->key), getpid ());
+
+
+ cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
+ cqr->key = get->key;
+ cqr->client = find_active_client (client);
+ cqr->xquery = (void *) &cqr[1];
+ memcpy (&cqr[1], xquery, xquery_size);
+ cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
+ cqr->retry_frequency = GNUNET_TIME_UNIT_SECONDS;
+ cqr->retry_time = GNUNET_TIME_absolute_get ();
+ cqr->unique_id = get->unique_id;
+ cqr->xquery_size = xquery_size;
+ cqr->replication = ntohl (get->desired_replication_level);
+ cqr->msg_options = ntohl (get->options);
+ cqr->type = ntohl (get->type);
+ // FIXME use cqr->key, set multihashmap create to GNUNET_YES
+ GNUNET_CONTAINER_multihashmap_put (forward_map, &get->key, cqr,
+
GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ GDS_CLIENTS_process_get (ntohl (get->options),
+ ntohl (get->type),
+ 0,
+ ntohl (get->desired_replication_level),
+ 1,
+ GDS_NEIGHBOURS_get_id(),
+ &get->key);
+ /* start remote requests */
+ if (GNUNET_SCHEDULER_NO_TASK != retry_task)
+ GNUNET_SCHEDULER_cancel (retry_task);
+ retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL);
+ /* perform local lookup */
+ GDS_DATACACHE_handle_get (&get->key, cqr->type, cqr->xquery, xquery_size,
+ NULL, 0);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
+/**
+ * Closure for 'find_by_unique_id'.
+ */
+struct FindByUniqueIdContext
+{
+ /**
+ * Where to store the result, if found.
+ */
+ struct ClientQueryRecord *cqr;
+
+ uint64_t unique_id;
+};
+
+
+/**
+ * Function called for each existing DHT record for the given
+ * query. Checks if it matches the UID given in the closure
+ * and if so returns the entry as a result.
+ *
+ * @param cls the search context
+ * @param key query for the lookup (not used)
+ * @param value the 'struct ClientQueryRecord'
+ * @return GNUNET_YES to continue iteration (result not yet found)
+ */
+static int
+find_by_unique_id (void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
+{
+ struct FindByUniqueIdContext *fui_ctx = cls;
+ struct ClientQueryRecord *cqr = value;
+
+ if (cqr->unique_id != fui_ctx->unique_id)
+ return GNUNET_YES;
+ fui_ctx->cqr = cqr;
+ return GNUNET_NO;
+}
+
+
+/**
+ * Handler for "GET result seen" messages from the client.
+ *
+ * @param cls closure for the service
+ * @param client the client we received this message from
+ * @param message the actual message received
+ */
+static void
+handle_dht_local_get_result_seen (void *cls, struct GNUNET_SERVER_Client
*client,
+ const struct GNUNET_MessageHeader *message)
+{
+ const struct GNUNET_DHT_ClientGetResultSeenMessage *seen;
+ uint16_t size;
+ unsigned int hash_count;
+ unsigned int old_count;
+ const struct GNUNET_HashCode *hc;
+ struct FindByUniqueIdContext fui_ctx;
+ struct ClientQueryRecord *cqr;
+
+ size = ntohs (message->size);
+ if (size < sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ seen = (const struct GNUNET_DHT_ClientGetResultSeenMessage *) message;
+ hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage))
/ sizeof (struct GNUNET_HashCode);
+ if (size != sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage) +
hash_count * sizeof (struct GNUNET_HashCode))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ hc = (const struct GNUNET_HashCode*) &seen[1];
+ fui_ctx.unique_id = seen->unique_id;
+ fui_ctx.cqr = NULL;
+ GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
+ &seen->key,
+ &find_by_unique_id,
+ &fui_ctx);
+ if (NULL == (cqr = fui_ctx.cqr))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ /* finally, update 'seen' list */
+ old_count = cqr->seen_replies_count;
+ GNUNET_array_grow (cqr->seen_replies,
+ cqr->seen_replies_count,
+ cqr->seen_replies_count + hash_count);
+ memcpy (&cqr->seen_replies[old_count],
+ hc,
+ sizeof (struct GNUNET_HashCode) * hash_count);
+}
+
+
+/**
+ * Closure for 'remove_by_unique_id'.
+ */
+struct RemoveByUniqueIdContext
+{
+ /**
+ * Client that issued the removal request.
+ */
+ struct ClientList *client;
+
+ /**
+ * Unique ID of the request.
+ */
+ uint64_t unique_id;
+};
+
+
+/**
+ * Iterator over hash map entries that frees all entries
+ * that match the given client and unique ID.
+ *
+ * @param cls unique ID and client to search for in source routes
+ * @param key current key code
+ * @param value value in the hash map, a ClientQueryRecord
+ * @return GNUNET_YES (we should continue to iterate)
+ */
+static int
+remove_by_unique_id (void *cls, const struct GNUNET_HashCode * key, void
*value)
+{
+ const struct RemoveByUniqueIdContext *ctx = cls;
+ struct ClientQueryRecord *record = value;
+
+ if (record->unique_id != ctx->unique_id)
+ return GNUNET_YES;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Removing client %p's record for key %s (by unique id)\n",
+ ctx->client->client_handle, GNUNET_h2s (key));
+ return remove_client_records (ctx->client, key, record);
+}
+
+
+/**
+ * Handler for any generic DHT stop messages, calls the appropriate handler
+ * depending on message type (if processed locally)
+ *
+ * @param cls closure for the service
+ * @param client the client we received this message from
+ * @param message the actual message received
+ *
+ */
+static void
+handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg =
+ (const struct GNUNET_DHT_ClientGetStopMessage *) message;
+ struct RemoveByUniqueIdContext ctx;
+
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# GET STOP requests received from clients"), 1,
+ GNUNET_NO);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received GET STOP request for %s from local client %p\n",
+ client, GNUNET_h2s (&dht_stop_msg->key));
+ ctx.client = find_active_client (client);
+ ctx.unique_id = dht_stop_msg->unique_id;
+ GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, &dht_stop_msg->key,
+ &remove_by_unique_id, &ctx);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
+/**
+ * Handler for monitor start messages
+ *
+ * @param cls closure for the service
+ * @param client the client we received this message from
+ * @param message the actual message received
+ *
+ */
+static void
+handle_dht_local_monitor (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct ClientMonitorRecord *r;
+ const struct GNUNET_DHT_MonitorStartStopMessage *msg;
+
+ msg = (struct GNUNET_DHT_MonitorStartStopMessage *) message;
+ r = GNUNET_malloc (sizeof(struct ClientMonitorRecord));
+
+ r->client = find_active_client(client);
+ r->type = ntohl(msg->type);
+ r->get = ntohs(msg->get);
+ r->get_resp = ntohs(msg->get_resp);
+ r->put = ntohs(msg->put);
+ if (0 == ntohs(msg->filter_key))
+ r->key = NULL;
+ else
+ {
+ r->key = GNUNET_malloc (sizeof (struct GNUNET_HashCode));
+ memcpy (r->key, &msg->key, sizeof (struct GNUNET_HashCode));
+ }
+ GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, r);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+/**
+ * Handler for monitor stop messages
+ *
+ * @param cls closure for the service
+ * @param client the client we received this message from
+ * @param message the actual message received
+ *
+ */
+static void
+handle_dht_local_monitor_stop (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct ClientMonitorRecord *r;
+ const struct GNUNET_DHT_MonitorStartStopMessage *msg;
+ int keys_match;
+
+ msg = (struct GNUNET_DHT_MonitorStartStopMessage *) message;
+ r = monitor_head;
+
+ while (NULL != r)
+ {
+ if (NULL == r->key)
+ keys_match = (0 == ntohs(msg->filter_key));
+ else
+ {
+ keys_match = (0 != ntohs(msg->filter_key)
+ && !memcmp(r->key, &msg->key, sizeof(struct
GNUNET_HashCode)));
+ }
+ if (find_active_client(client) == r->client
+ && ntohl(msg->type) == r->type
+ && r->get == msg->get
+ && r->get_resp == msg->get_resp
+ && r->put == msg->put
+ && keys_match
+ )
+ {
+ GNUNET_CONTAINER_DLL_remove (monitor_head, monitor_tail, r);
+ GNUNET_free_non_null (r->key);
+ GNUNET_free (r);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return; /* Delete only ONE entry */
+ }
+ r = r->next;
+ }
+
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
+/**
+ * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
+ * request. A ClientList is passed as closure, take the head of the list
+ * and copy it into buf, which has the result of sending the message to the
+ * client.
+ *
+ * @param cls closure to this call
+ * @param size maximum number of bytes available to send
+ * @param buf where to copy the actual message to
+ *
+ * @return the number of bytes actually copied, 0 indicates failure
+ */
+static size_t
+send_reply_to_client (void *cls, size_t size, void *buf)
+{
+ struct ClientList *client = cls;
+ char *cbuf = buf;
+ struct PendingMessage *reply;
+ size_t off;
+ size_t msize;
+
+ client->transmit_handle = NULL;
+ if (buf == NULL)
+ {
+ /* client disconnected */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client %p disconnected, pending messages will be discarded\n",
+ client->client_handle);
+ return 0;
+ }
+ off = 0;
+ while ((NULL != (reply = client->pending_head)) &&
+ (size >= off + (msize = ntohs (reply->msg->size))))
+ {
+ GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
+ reply);
+ memcpy (&cbuf[off], reply->msg, msize);
+ GNUNET_free (reply);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to client
%p\n",
+ msize, client->client_handle);
+ off += msize;
+ }
+ process_pending_messages (client);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %u/%u bytes to client
%p\n",
+ (unsigned int) off, (unsigned int) size, client->client_handle);
+ return off;
+}
+
+
+/**
+ * Task run to check for messages that need to be sent to a client.
+ *
+ * @param client a ClientList, containing the client and any messages to be
sent to it
+ */
+static void
+process_pending_messages (struct ClientList *client)
+{
+ if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Not asking for transmission to %p now: %s\n",
+ client->client_handle,
+ client->pending_head ==
+ NULL ? "no more messages" : "request already pending");
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Asking for transmission of %u bytes to client %p\n",
+ ntohs (client->pending_head->msg->size), client->client_handle);
+ client->transmit_handle =
+ GNUNET_SERVER_notify_transmit_ready (client->client_handle,
+ ntohs (client->pending_head->
+ msg->size),
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ &send_reply_to_client, client);
+}
+
+
+/**
+ * Closure for 'forward_reply'
+ */
+struct ForwardReplyContext
+{
+
+ /**
+ * Actual message to send to matching clients.
+ */
+ struct PendingMessage *pm;
+
+ /**
+ * Embedded payload.
+ */
+ const void *data;
+
+ /**
+ * Type of the data.
+ */
+ enum GNUNET_BLOCK_Type type;
+
+ /**
+ * Number of bytes in data.
+ */
+ size_t data_size;
+
+ /**
+ * Do we need to copy 'pm' because it was already used?
+ */
+ int do_copy;
+
+};
+
+
+/**
+ * Iterator over hash map entries that send a given reply to
+ * each of the matching clients. With some tricky recycling
+ * of the buffer.
+ *
+ * @param cls the 'struct ForwardReplyContext'
+ * @param key current key
+ * @param value value in the hash map, a ClientQueryRecord
+ * @return GNUNET_YES (we should continue to iterate),
+ * if the result is mal-formed, GNUNET_NO
+ */
+static int
+forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
+{
+ struct ForwardReplyContext *frc = cls;
+ struct ClientQueryRecord *record = value;
+ struct PendingMessage *pm;
+ struct GNUNET_DHT_ClientResultMessage *reply;
+ enum GNUNET_BLOCK_EvaluationResult eval;
+ int do_free;
+ struct GNUNET_HashCode ch;
+ unsigned int i;
+
+ LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
+ "XDHT CLIENT-RESULT %s\n",
+ GNUNET_h2s (key));
+ if ((record->type != GNUNET_BLOCK_TYPE_ANY) && (record->type != frc->type))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Record type missmatch, not passing request for key %s to local
client\n",
+ GNUNET_h2s (key));
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Key match, type mismatches in REPLY to
CLIENT"),
+ 1, GNUNET_NO);
+ return GNUNET_YES; /* type mismatch */
+ }
+ GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch);
+ for (i = 0; i < record->seen_replies_count; i++)
+ if (0 == memcmp (&record->seen_replies[i], &ch, sizeof (struct
GNUNET_HashCode)))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Duplicate reply, not passing request for key %s to local client\n",
+ GNUNET_h2s (key));
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Duplicate REPLIES to CLIENT request
dropped"),
+ 1, GNUNET_NO);
+ return GNUNET_YES; /* duplicate */
+ }
+ eval =
+ GNUNET_BLOCK_evaluate (GDS_block_context, record->type, key, NULL, 0,
+ record->xquery, record->xquery_size, frc->data,
+ frc->data_size);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Evaluation result is %d for key %s for local client's query\n",
+ (int) eval, GNUNET_h2s (key));
+ switch (eval)
+ {
+ case GNUNET_BLOCK_EVALUATION_OK_LAST:
+ do_free = GNUNET_YES;
+ break;
+ case GNUNET_BLOCK_EVALUATION_OK_MORE:
+ GNUNET_array_append (record->seen_replies, record->seen_replies_count, ch);
+ do_free = GNUNET_NO;
+ break;
+ case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
+ /* should be impossible to encounter here */
+ GNUNET_break (0);
+ return GNUNET_YES;
+ case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
+ GNUNET_break_op (0);
+ return GNUNET_NO;
+ case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
+ GNUNET_break (0);
+ return GNUNET_NO;
+ case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
+ GNUNET_break (0);
+ return GNUNET_NO;
+ case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
+ return GNUNET_YES;
+ case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Unsupported block type (%u) in request!\n"), record->type);
+ return GNUNET_NO;
+ default:
+ GNUNET_break (0);
+ return GNUNET_NO;
+ }
+ if (GNUNET_NO == frc->do_copy)
+ {
+ /* first time, we can use the original data */
+ pm = frc->pm;
+ frc->do_copy = GNUNET_YES;
+ }
+ else
+ {
+ /* two clients waiting for same reply, must copy for queueing */
+ pm = GNUNET_malloc (sizeof (struct PendingMessage) +
+ ntohs (frc->pm->msg->size));
+ memcpy (pm, frc->pm,
+ sizeof (struct PendingMessage) + ntohs (frc->pm->msg->size));
+ pm->next = pm->prev = NULL;
+ pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
+ }
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# RESULTS queued for clients"), 1,
+ GNUNET_NO);
+ reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
+ reply->unique_id = record->unique_id;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Queueing reply to query %s for client %p\n",
+ GNUNET_h2s (key),
+ record->client->client_handle);
+ add_pending_message (record->client, pm);
+ if (GNUNET_YES == do_free)
+ remove_client_records (record->client, key, record);
+ return GNUNET_YES;
+}
+
+
+/**
+ * Handle a reply we've received from another peer. If the reply
+ * matches any of our pending queries, forward it to the respective
+ * client(s).
+ *
+ * @param expiration when will the reply expire
+ * @param key the query this reply is for
+ * @param get_path_length number of peers in @a get_path
+ * @param get_path path the reply took on get
+ * @param put_path_length number of peers in @a put_path
+ * @param put_path path the reply took on put
+ * @param type type of the reply
+ * @param data_size number of bytes in @a data
+ * @param data application payload data
+ */
+void
+GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
+ const struct GNUNET_HashCode *key,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *get_path,
+ unsigned int put_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ enum GNUNET_BLOCK_Type type, size_t data_size,
+ const void *data)
+{
+ struct ForwardReplyContext frc;
+ struct PendingMessage *pm;
+ struct GNUNET_DHT_ClientResultMessage *reply;
+ struct GNUNET_PeerIdentity *paths;
+ size_t msize;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "reply for key %s\n",
+ GNUNET_h2s (key));
+
+ if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map, key))
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# REPLIES ignored for CLIENTS (no match)"), 1,
+ GNUNET_NO);
+ return; /* no matching request, fast exit! */
+ }
+ msize =
+ sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size +
+ (get_path_length + put_path_length) * sizeof (struct
GNUNET_PeerIdentity);
+ if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Could not pass reply to client, message too big!\n"));
+ return;
+ }
+ pm = GNUNET_malloc (msize + sizeof (struct PendingMessage));
+ reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
+ pm->msg = &reply->header;
+ reply->header.size = htons ((uint16_t) msize);
+ reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
+ reply->type = htonl (type);
+ reply->get_path_length = htonl (get_path_length);
+ reply->put_path_length = htonl (put_path_length);
+ reply->unique_id = 0; /* filled in later */
+ reply->expiration = GNUNET_TIME_absolute_hton (expiration);
+ reply->key = *key;
+ paths = (struct GNUNET_PeerIdentity *) &reply[1];
+ memcpy (paths, put_path,
+ sizeof (struct GNUNET_PeerIdentity) * put_path_length);
+ memcpy (&paths[put_path_length], get_path,
+ sizeof (struct GNUNET_PeerIdentity) * get_path_length);
+ memcpy (&paths[get_path_length + put_path_length], data, data_size);
+ frc.do_copy = GNUNET_NO;
+ frc.pm = pm;
+ frc.data = data;
+ frc.data_size = data_size;
+ frc.type = type;
+ GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, key, &forward_reply,
+ &frc);
+ if (GNUNET_NO == frc.do_copy)
+ {
+ /* did not match any of the requests, free! */
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# REPLIES ignored for CLIENTS (no match)"), 1,
+ GNUNET_NO);
+ GNUNET_free (pm);
+ }
+}
+
+
+/**
+ * Check if some client is monitoring GET messages and notify
+ * them in that case.
+ *
+ * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
+ * @param type The type of data in the request.
+ * @param hop_count Hop count so far.
+ * @param path_length number of entries in path (or 0 if not recorded).
+ * @param path peers on the GET path (or NULL if not recorded).
+ * @param desired_replication_level Desired replication level.
+ * @param key Key of the requested data.
+ */
+void
+GDS_CLIENTS_process_get (uint32_t options,
+ enum GNUNET_BLOCK_Type type,
+ uint32_t hop_count,
+ uint32_t desired_replication_level,
+ unsigned int path_length,
+ const struct GNUNET_PeerIdentity *path,
+ const struct GNUNET_HashCode * key)
+{
+ struct ClientMonitorRecord *m;
+ struct ClientList **cl;
+ unsigned int cl_size;
+
+ cl = NULL;
+ cl_size = 0;
+ for (m = monitor_head; NULL != m; m = m->next)
+ {
+ if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
+ (NULL == m->key ||
+ memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
+ {
+ struct PendingMessage *pm;
+ struct GNUNET_DHT_MonitorGetMessage *mmsg;
+ struct GNUNET_PeerIdentity *msg_path;
+ size_t msize;
+ unsigned int i;
+
+ /* Don't send duplicates */
+ for (i = 0; i < cl_size; i++)
+ if (cl[i] == m->client)
+ break;
+ if (i < cl_size)
+ continue;
+ GNUNET_array_append (cl, cl_size, m->client);
+
+ msize = path_length * sizeof (struct GNUNET_PeerIdentity);
+ msize += sizeof (struct GNUNET_DHT_MonitorGetMessage);
+ msize += sizeof (struct PendingMessage);
+ pm = GNUNET_malloc (msize);
+ mmsg = (struct GNUNET_DHT_MonitorGetMessage *) &pm[1];
+ pm->msg = &mmsg->header;
+ mmsg->header.size = htons (msize - sizeof (struct PendingMessage));
+ mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET);
+ mmsg->options = htonl(options);
+ mmsg->type = htonl(type);
+ mmsg->hop_count = htonl(hop_count);
+ mmsg->desired_replication_level = htonl(desired_replication_level);
+ mmsg->get_path_length = htonl(path_length);
+ memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
+ msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
+ if (path_length > 0)
+ memcpy (msg_path, path,
+ path_length * sizeof (struct GNUNET_PeerIdentity));
+ add_pending_message (m->client, pm);
+ }
+ }
+ GNUNET_free_non_null (cl);
+}
+
+
+/**
+ * Check if some client is monitoring GET RESP messages and notify
+ * them in that case.
+ *
+ * @param type The type of data in the result.
+ * @param get_path Peers on GET path (or NULL if not recorded).
+ * @param get_path_length number of entries in get_path.
+ * @param put_path peers on the PUT path (or NULL if not recorded).
+ * @param put_path_length number of entries in get_path.
+ * @param exp Expiration time of the data.
+ * @param key Key of the data.
+ * @param data Pointer to the result data.
+ * @param size Number of bytes in data.
+ */
+void
+GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type,
+ const struct GNUNET_PeerIdentity *get_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int put_path_length,
+ struct GNUNET_TIME_Absolute exp,
+ const struct GNUNET_HashCode * key,
+ const void *data,
+ size_t size)
+{
+ struct ClientMonitorRecord *m;
+ struct ClientList **cl;
+ unsigned int cl_size;
+
+ cl = NULL;
+ cl_size = 0;
+ for (m = monitor_head; NULL != m; m = m->next)
+ {
+ if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
+ (NULL == m->key ||
+ memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
+ {
+ struct PendingMessage *pm;
+ struct GNUNET_DHT_MonitorGetRespMessage *mmsg;
+ struct GNUNET_PeerIdentity *path;
+ size_t msize;
+ unsigned int i;
+
+ /* Don't send duplicates */
+ for (i = 0; i < cl_size; i++)
+ if (cl[i] == m->client)
+ break;
+ if (i < cl_size)
+ continue;
+ GNUNET_array_append (cl, cl_size, m->client);
+
+ msize = size;
+ msize += (get_path_length + put_path_length)
+ * sizeof (struct GNUNET_PeerIdentity);
+ msize += sizeof (struct GNUNET_DHT_MonitorGetRespMessage);
+ msize += sizeof (struct PendingMessage);
+ pm = GNUNET_malloc (msize);
+ mmsg = (struct GNUNET_DHT_MonitorGetRespMessage *) &pm[1];
+ pm->msg = (struct GNUNET_MessageHeader *) mmsg;
+ mmsg->header.size = htons (msize - sizeof (struct PendingMessage));
+ mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP);
+ mmsg->type = htonl(type);
+ mmsg->put_path_length = htonl(put_path_length);
+ mmsg->get_path_length = htonl(get_path_length);
+ path = (struct GNUNET_PeerIdentity *) &mmsg[1];
+ if (put_path_length > 0)
+ {
+ memcpy (path, put_path,
+ put_path_length * sizeof (struct GNUNET_PeerIdentity));
+ path = &path[put_path_length];
+ }
+ if (get_path_length > 0)
+ memcpy (path, get_path,
+ get_path_length * sizeof (struct GNUNET_PeerIdentity));
+ mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
+ memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
+ if (size > 0)
+ memcpy (&path[get_path_length], data, size);
+ add_pending_message (m->client, pm);
+ }
+ }
+ GNUNET_free_non_null (cl);
+}
+
+
+/**
+ * Check if some client is monitoring PUT messages and notify
+ * them in that case.
+ *
+ * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
+ * @param type The type of data in the request.
+ * @param hop_count Hop count so far.
+ * @param path_length number of entries in path (or 0 if not recorded).
+ * @param path peers on the PUT path (or NULL if not recorded).
+ * @param desired_replication_level Desired replication level.
+ * @param exp Expiration time of the data.
+ * @param key Key under which data is to be stored.
+ * @param data Pointer to the data carried.
+ * @param size Number of bytes in data.
+ */
+void
+GDS_CLIENTS_process_put (uint32_t options,
+ enum GNUNET_BLOCK_Type type,
+ uint32_t hop_count,
+ uint32_t desired_replication_level,
+ unsigned int path_length,
+ const struct GNUNET_PeerIdentity *path,
+ struct GNUNET_TIME_Absolute exp,
+ const struct GNUNET_HashCode * key,
+ const void *data,
+ size_t size)
+{
+ struct ClientMonitorRecord *m;
+ struct ClientList **cl;
+ unsigned int cl_size;
+
+ cl = NULL;
+ cl_size = 0;
+ for (m = monitor_head; NULL != m; m = m->next)
+ {
+ if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
+ (NULL == m->key ||
+ memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
+ {
+ struct PendingMessage *pm;
+ struct GNUNET_DHT_MonitorPutMessage *mmsg;
+ struct GNUNET_PeerIdentity *msg_path;
+ size_t msize;
+ unsigned int i;
+
+ /* Don't send duplicates */
+ for (i = 0; i < cl_size; i++)
+ if (cl[i] == m->client)
+ break;
+ if (i < cl_size)
+ continue;
+ GNUNET_array_append (cl, cl_size, m->client);
+
+ msize = size;
+ msize += path_length * sizeof (struct GNUNET_PeerIdentity);
+ msize += sizeof (struct GNUNET_DHT_MonitorPutMessage);
+ msize += sizeof (struct PendingMessage);
+ pm = GNUNET_malloc (msize);
+ mmsg = (struct GNUNET_DHT_MonitorPutMessage *) &pm[1];
+ pm->msg = (struct GNUNET_MessageHeader *) mmsg;
+ mmsg->header.size = htons (msize - sizeof (struct PendingMessage));
+ mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT);
+ mmsg->options = htonl(options);
+ mmsg->type = htonl(type);
+ mmsg->hop_count = htonl(hop_count);
+ mmsg->desired_replication_level = htonl(desired_replication_level);
+ mmsg->put_path_length = htonl(path_length);
+ msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
+ if (path_length > 0)
+ {
+ memcpy (msg_path, path,
+ path_length * sizeof (struct GNUNET_PeerIdentity));
+ }
+ mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
+ memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
+ if (size > 0)
+ memcpy (&msg_path[path_length], data, size);
+ add_pending_message (m->client, pm);
+ }
+ }
+ GNUNET_free_non_null (cl);
+}
+
+
+/**
+ * Initialize client subsystem.
+ *
+ * @param server the initialized server
+ */
+void
+GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
+{
+ static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
+ {&handle_dht_local_put, NULL,
+ GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, 0},
+ {&handle_dht_local_get, NULL,
+ GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, 0},
+ {&handle_dht_local_get_stop, NULL,
+ GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP,
+ sizeof (struct GNUNET_DHT_ClientGetStopMessage)},
+ {&handle_dht_local_monitor, NULL,
+ GNUNET_MESSAGE_TYPE_DHT_MONITOR_START,
+ sizeof (struct GNUNET_DHT_MonitorStartStopMessage)},
+ {&handle_dht_local_monitor_stop, NULL,
+ GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP,
+ sizeof (struct GNUNET_DHT_MonitorStartStopMessage)},
+ {&handle_dht_local_get_result_seen, NULL,
+ GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN, 0},
+ {NULL, NULL, 0, 0}
+ };
+ forward_map = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
+ retry_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
+ GNUNET_SERVER_add_handlers (server, plugin_handlers);
+ GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
+}
+
+
+/**
+ * Shutdown client subsystem.
+ */
+void
+GDS_CLIENTS_done ()
+{
+ GNUNET_assert (client_head == NULL);
+ GNUNET_assert (client_tail == NULL);
+ if (GNUNET_SCHEDULER_NO_TASK != retry_task)
+ {
+ GNUNET_SCHEDULER_cancel (retry_task);
+ retry_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (NULL != retry_heap)
+ {
+ GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
+ GNUNET_CONTAINER_heap_destroy (retry_heap);
+ retry_heap = NULL;
+ }
+ if (NULL != forward_map)
+ {
+ GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
+ GNUNET_CONTAINER_multihashmap_destroy (forward_map);
+ forward_map = NULL;
+ }
+}
+
+/* end of gnunet-service-dht_clients.c */
Copied: gnunet/src/dht/gnunet-service-xdht_clients.h (from rev 31026,
gnunet/src/dht/gnunet-service-dht_clients.h)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_clients.h
(rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_clients.h 2013-12-05 10:25:18 UTC
(rev 31062)
@@ -0,0 +1,147 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_clients.h
+ * @brief GNUnet DHT service's client management code
+ * @author Christian Grothoff
+ * @author Nathan Evans
+ */
+#ifndef GNUNET_SERVICE_DHT_CLIENT_H
+#define GNUNET_SERVICE_DHT_CLIENT_H
+
+#include "gnunet_util_lib.h"
+#include "gnunet_block_lib.h"
+
+/**
+ * Handle a reply we've received from another peer. If the reply
+ * matches any of our pending queries, forward it to the respective
+ * client(s).
+ *
+ * @param expiration when will the reply expire
+ * @param key the query this reply is for
+ * @param get_path_length number of peers in 'get_path'
+ * @param get_path path the reply took on get
+ * @param put_path_length number of peers in 'put_path'
+ * @param put_path path the reply took on put
+ * @param type type of the reply
+ * @param data_size number of bytes in 'data'
+ * @param data application payload data
+ */
+void
+GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
+ const struct GNUNET_HashCode * key,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *get_path,
+ unsigned int put_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ enum GNUNET_BLOCK_Type type, size_t data_size,
+ const void *data);
+
+
+/**
+ * Check if some client is monitoring GET messages and notify
+ * them in that case.
+ *
+ * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
+ * @param type The type of data in the request.
+ * @param hop_count Hop count so far.
+ * @param path_length number of entries in path (or 0 if not recorded).
+ * @param path peers on the GET path (or NULL if not recorded).
+ * @param desired_replication_level Desired replication level.
+ * @param key Key of the requested data.
+ */
+void
+GDS_CLIENTS_process_get (uint32_t options,
+ enum GNUNET_BLOCK_Type type,
+ uint32_t hop_count,
+ uint32_t desired_replication_level,
+ unsigned int path_length,
+ const struct GNUNET_PeerIdentity *path,
+ const struct GNUNET_HashCode * key);
+
+/**
+ * Check if some client is monitoring GET RESP messages and notify
+ * them in that case.
+ *
+ * @param type The type of data in the result.
+ * @param get_path Peers on GET path (or NULL if not recorded).
+ * @param get_path_length number of entries in get_path.
+ * @param put_path peers on the PUT path (or NULL if not recorded).
+ * @param put_path_length number of entries in get_path.
+ * @param exp Expiration time of the data.
+ * @param key Key of the data.
+ * @param data Pointer to the result data.
+ * @param size Number of bytes in data.
+ */
+void
+GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type,
+ const struct GNUNET_PeerIdentity *get_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int put_path_length,
+ struct GNUNET_TIME_Absolute exp,
+ const struct GNUNET_HashCode * key,
+ const void *data,
+ size_t size);
+
+/**
+ * Check if some client is monitoring PUT messages and notify
+ * them in that case.
+ *
+ * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
+ * @param type The type of data in the request.
+ * @param hop_count Hop count so far.
+ * @param path_length number of entries in path (or 0 if not recorded).
+ * @param path peers on the PUT path (or NULL if not recorded).
+ * @param desired_replication_level Desired replication level.
+ * @param exp Expiration time of the data.
+ * @param key Key under which data is to be stored.
+ * @param data Pointer to the data carried.
+ * @param size Number of bytes in data.
+ */
+void
+GDS_CLIENTS_process_put (uint32_t options,
+ enum GNUNET_BLOCK_Type type,
+ uint32_t hop_count,
+ uint32_t desired_replication_level,
+ unsigned int path_length,
+ const struct GNUNET_PeerIdentity *path,
+ struct GNUNET_TIME_Absolute exp,
+ const struct GNUNET_HashCode * key,
+ const void *data,
+ size_t size);
+
+/**
+ * Initialize client subsystem.
+ *
+ * @param server the initialized server
+ */
+void
+GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server);
+
+
+/**
+ * Shutdown client subsystem.
+ */
+void
+GDS_CLIENTS_done (void);
+
+#endif
Copied: gnunet/src/dht/gnunet-service-xdht_datacache.c (from rev 31026,
gnunet/src/dht/gnunet-service-dht_datacache.c)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_datacache.c
(rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_datacache.c 2013-12-05 10:25:18 UTC
(rev 31062)
@@ -0,0 +1,275 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_datacache.c
+ * @brief GNUnet DHT service's datacache integration
+ * @author Christian Grothoff
+ * @author Nathan Evans
+ */
+#include "platform.h"
+#include "gnunet_datacache_lib.h"
+#include "gnunet-service-dht_clients.h"
+#include "gnunet-service-dht_datacache.h"
+#include "gnunet-service-dht_routing.h"
+#include "gnunet-service-dht.h"
+
+#define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__VA_ARGS__)
+
+
+/**
+ * Handle to the datacache service (for inserting/retrieving data)
+ */
+static struct GNUNET_DATACACHE_Handle *datacache;
+
+
+/**
+ * Handle a datum we've received from another peer. Cache if
+ * possible.
+ *
+ * @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 path the reply took on put
+ * @param type type of the reply
+ * @param data_size number of bytes in 'data'
+ * @param data application payload data
+ */
+void
+GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
+ 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,
+ const void *data)
+{
+ int r;
+
+ if (NULL == datacache)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("%s request received, but have no datacache!\n"), "PUT");
+ return;
+ }
+ if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ /* 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);
+ r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
+ put_path_length, put_path);
+ 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);
+}
+
+
+/**
+ * Context containing information about a GET request.
+ */
+struct GetRequestContext
+{
+ /**
+ * extended query (see gnunet_block_lib.h).
+ */
+ const void *xquery;
+
+ /**
+ * Bloomfilter to filter out duplicate replies (updated)
+ */
+ struct GNUNET_CONTAINER_BloomFilter **reply_bf;
+
+ /**
+ * The key this request was about
+ */
+ struct GNUNET_HashCode key;
+
+ /**
+ * Number of bytes in xquery.
+ */
+ size_t xquery_size;
+
+ /**
+ * Mutator value for the reply_bf, see gnunet_block_lib.h
+ */
+ uint32_t reply_bf_mutator;
+
+ /**
+ * Return value to give back.
+ */
+ enum GNUNET_BLOCK_EvaluationResult eval;
+};
+
+
+/**
+ * Iterator for local get request results,
+ *
+ * @param cls closure for iterator, a DatacacheGetContext
+ * @param exp when does this value expire?
+ * @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 put_path_length number of peers in '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_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)
+{
+ struct GetRequestContext *ctx = cls;
+ enum GNUNET_BLOCK_EvaluationResult eval;
+
+ eval =
+ GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
+ ctx->reply_bf_mutator, ctx->xquery,
+ ctx->xquery_size, data, size);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Found reply for query %s in datacache, evaluation result is %d\n",
+ GNUNET_h2s (key), (int) eval);
+ ctx->eval = eval;
+ switch (eval)
+ {
+ case GNUNET_BLOCK_EVALUATION_OK_MORE:
+ case GNUNET_BLOCK_EVALUATION_OK_LAST:
+ /* forward to local clients */
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Good RESULTS found in datacache"), 1,
+ GNUNET_NO);
+ GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, put_path,
+ type, size, data);
+ /* forward to other peers */
+ GDS_ROUTING_process (type, exp, key, put_path_length, put_path, 0, NULL,
+ data, size);
+ break;
+ case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Duplicate RESULTS found in datacache"), 1,
+ GNUNET_NO);
+ break;
+ case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Invalid RESULTS found in datacache"), 1,
+ GNUNET_NO);
+ break;
+ case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Irrelevant RESULTS found in datacache"), 1,
+ GNUNET_NO);
+ break;
+ case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
+ GNUNET_break (0);
+ break;
+ case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Unsupported RESULTS found in datacache"), 1,
+ GNUNET_NO);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Unsupported block type (%u) in local response!\n"), type);
+ break;
+ }
+ return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
+}
+
+
+/**
+ * Handle a GET request we've received from another peer.
+ *
+ * @param key the query
+ * @param type requested data type
+ * @param xquery extended query
+ * @param xquery_size number of bytes in 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
+ * @return evaluation result for the local replies
+ */
+enum GNUNET_BLOCK_EvaluationResult
+GDS_DATACACHE_handle_get (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)
+{
+ struct GetRequestContext ctx;
+ unsigned int r;
+
+ if (datacache == NULL)
+ 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.key = *key;
+ ctx.xquery = xquery;
+ ctx.xquery_size = xquery_size;
+ ctx.reply_bf = reply_bf;
+ ctx.reply_bf_mutator = reply_bf_mutator;
+ r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
+ &ctx);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "DATACACHE GET for key %s completed (%d). %u results found.\n",
+ GNUNET_h2s (key), ctx.eval, r);
+ return ctx.eval;
+}
+
+
+/**
+ * Initialize datacache subsystem.
+ */
+void
+GDS_DATACACHE_init ()
+{
+ datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
+}
+
+
+/**
+ * Shutdown datacache subsystem.
+ */
+void
+GDS_DATACACHE_done ()
+{
+ if (datacache != NULL)
+ {
+ GNUNET_DATACACHE_destroy (datacache);
+ datacache = NULL;
+ }
+}
+
+
+/* end of gnunet-service-dht_datacache.c */
Copied: gnunet/src/dht/gnunet-service-xdht_datacache.h (from rev 31026,
gnunet/src/dht/gnunet-service-dht_datacache.h)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_datacache.h
(rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_datacache.h 2013-12-05 10:25:18 UTC
(rev 31062)
@@ -0,0 +1,86 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_datacache.h
+ * @brief GNUnet DHT service's datacache integration
+ * @author Christian Grothoff
+ * @author Nathan Evans
+ */
+#ifndef GNUNET_SERVICE_DHT_DATACACHE_H
+#define GNUNET_SERVICE_DHT_DATACACHE_H
+
+#include "gnunet_util_lib.h"
+#include "gnunet_block_lib.h"
+
+/**
+ * Handle a datum we've received from another peer. Cache if
+ * possible.
+ *
+ * @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 path the reply took on put
+ * @param type type of the reply
+ * @param data_size number of bytes in 'data'
+ * @param data application payload data
+ */
+void
+GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
+ 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,
+ const void *data);
+
+
+/**
+ * Handle a GET request we've received from another peer.
+ *
+ * @param key the query
+ * @param type requested data type
+ * @param xquery extended query
+ * @param xquery_size number of bytes in 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
+ * @return evaluation result for the local replies
+ */
+enum GNUNET_BLOCK_EvaluationResult
+GDS_DATACACHE_handle_get (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);
+
+
+/**
+ * Initialize datacache subsystem.
+ */
+void
+GDS_DATACACHE_init (void);
+
+
+/**
+ * Shutdown datacache subsystem.
+ */
+void
+GDS_DATACACHE_done (void);
+
+#endif
Copied: gnunet/src/dht/gnunet-service-xdht_hello.c (from rev 31026,
gnunet/src/dht/gnunet-service-dht_hello.c)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_hello.c (rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_hello.c 2013-12-05 10:25:18 UTC (rev
31062)
@@ -0,0 +1,137 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_hello.c
+ * @brief GNUnet DHT integration with peerinfo
+ * @author Christian Grothoff
+ *
+ * TODO:
+ * - consider adding mechanism to remove expired HELLOs
+ */
+#include "platform.h"
+#include "gnunet-service-dht.h"
+#include "gnunet-service-dht_hello.h"
+#include "gnunet_peerinfo_service.h"
+
+
+/**
+ * Handle for peerinfo notifications.
+ */
+static struct GNUNET_PEERINFO_NotifyContext *pnc;
+
+/**
+ * Hash map of peers to HELLOs.
+ */
+static struct GNUNET_CONTAINER_MultiPeerMap *peer_to_hello;
+
+
+/**
+ * Obtain a peer's HELLO if available
+ *
+ * @param peer peer to look for a HELLO from
+ * @return HELLO for the given peer
+ */
+const struct GNUNET_HELLO_Message *
+GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer)
+{
+ if (NULL == peer_to_hello)
+ return NULL;
+ return GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
+}
+
+
+/**
+ * Function called for each HELLO known to PEERINFO.
+ *
+ * @param cls closure
+ * @param peer id of the peer, NULL for last call
+ * @param hello hello message for the peer (can be NULL)
+ * @param err_msg error message (not used)
+ */
+static void
+process_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_HELLO_Message *hello, const char *err_msg)
+{
+ struct GNUNET_TIME_Absolute ex;
+ struct GNUNET_HELLO_Message *hm;
+
+ if (hello == NULL)
+ return;
+ ex = GNUNET_HELLO_get_last_expiration (hello);
+ if (0 == GNUNET_TIME_absolute_get_remaining (ex).rel_value_us)
+ return;
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# HELLOs obtained from peerinfo"),
1,
+ GNUNET_NO);
+ hm = GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
+ GNUNET_free_non_null (hm);
+ hm = GNUNET_malloc (GNUNET_HELLO_size (hello));
+ memcpy (hm, hello, GNUNET_HELLO_size (hello));
+ GNUNET_assert (GNUNET_SYSERR !=
+ GNUNET_CONTAINER_multipeermap_put (peer_to_hello,
+ peer, hm,
+
GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
+}
+
+
+/**
+ * Initialize HELLO subsystem.
+ */
+void
+GDS_HELLO_init ()
+{
+ pnc = GNUNET_PEERINFO_notify (GDS_cfg, GNUNET_NO, &process_hello, NULL);
+ peer_to_hello = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
+}
+
+
+/**
+ * Free memory occopied by the HELLO.
+ */
+static int
+free_hello (void *cls,
+ const struct GNUNET_PeerIdentity *key,
+ void *hello)
+{
+ GNUNET_free (hello);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Shutdown HELLO subsystem.
+ */
+void
+GDS_HELLO_done ()
+{
+ if (NULL != pnc)
+ {
+ GNUNET_PEERINFO_notify_cancel (pnc);
+ pnc = NULL;
+ }
+ if (NULL != peer_to_hello)
+ {
+ GNUNET_CONTAINER_multipeermap_iterate (peer_to_hello, &free_hello, NULL);
+ GNUNET_CONTAINER_multipeermap_destroy (peer_to_hello);
+ }
+}
+
+/* end of gnunet-service-dht_hello.c */
Copied: gnunet/src/dht/gnunet-service-xdht_hello.h (from rev 31026,
gnunet/src/dht/gnunet-service-dht_hello.h)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_hello.h (rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_hello.h 2013-12-05 10:25:18 UTC (rev
31062)
@@ -0,0 +1,55 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_hello.h
+ * @brief GNUnet DHT integration with peerinfo
+ * @author Christian Grothoff
+ */
+#ifndef GNUNET_SERVICE_DHT_HELLO_H
+#define GNUNET_SERVICE_DHT_HELLO_H
+
+#include "gnunet_util_lib.h"
+#include "gnunet_hello_lib.h"
+
+/**
+ * Obtain a peer's HELLO if available
+ *
+ * @param peer peer to look for a HELLO from
+ * @return HELLO for the given peer
+ */
+const struct GNUNET_HELLO_Message *
+GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer);
+
+
+/**
+ * Initialize HELLO subsystem.
+ */
+void
+GDS_HELLO_init (void);
+
+
+/**
+ * Shutdown HELLO subsystem.
+ */
+void
+GDS_HELLO_done (void);
+
+#endif
Copied: gnunet/src/dht/gnunet-service-xdht_neighbours.c (from rev 31026,
gnunet/src/dht/gnunet-service-dht_neighbours.c)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_neighbours.c
(rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_neighbours.c 2013-12-05 10:25:18 UTC
(rev 31062)
@@ -0,0 +1,2227 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009-2013 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_neighbours.c
+ * @brief GNUnet DHT service's bucket and neighbour management code
+ * @author Christian Grothoff
+ * @author Nathan Evans
+ */
+
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_block_lib.h"
+#include "gnunet_hello_lib.h"
+#include "gnunet_constants.h"
+#include "gnunet_protocols.h"
+#include "gnunet_nse_service.h"
+#include "gnunet_ats_service.h"
+#include "gnunet_core_service.h"
+#include "gnunet_datacache_lib.h"
+#include "gnunet_transport_service.h"
+#include "gnunet_hello_lib.h"
+#include "gnunet_dht_service.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet-service-dht.h"
+#include "gnunet-service-dht_clients.h"
+#include "gnunet-service-dht_datacache.h"
+#include "gnunet-service-dht_hello.h"
+#include "gnunet-service-dht_neighbours.h"
+#include "gnunet-service-dht_nse.h"
+#include "gnunet-service-dht_routing.h"
+#include <fenv.h>
+#include "dht.h"
+
+#define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
+
+/**
+ * How many buckets will we allow total.
+ */
+#define MAX_BUCKETS sizeof (struct GNUNET_HashCode) * 8
+
+/**
+ * What is the maximum number of peers in a given bucket.
+ */
+#define DEFAULT_BUCKET_SIZE 8
+
+/**
+ * Desired replication level for FIND PEER requests
+ */
+#define FIND_PEER_REPLICATION_LEVEL 4
+
+/**
+ * Maximum allowed replication level for all requests.
+ */
+#define MAXIMUM_REPLICATION_LEVEL 16
+
+/**
+ * Maximum allowed number of pending messages per peer.
+ */
+#define MAXIMUM_PENDING_PER_PEER 64
+
+/**
+ * How often to update our preference levels for peers in our routing tables.
+ */
+#define DHT_DEFAULT_PREFERENCE_INTERVAL
GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
+
+/**
+ * How long at least to wait before sending another find peer request.
+ */
+#define DHT_MINIMUM_FIND_PEER_INTERVAL
GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
+
+/**
+ * How long at most to wait before sending another find peer request.
+ */
+#define DHT_MAXIMUM_FIND_PEER_INTERVAL
GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10)
+
+/**
+ * How long at most to wait for transmission of a GET request to another peer?
+ */
+#define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
+
+/**
+ * Hello address expiration
+ */
+extern struct GNUNET_TIME_Relative hello_expiration;
+
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
+/**
+ * P2P PUT message
+ */
+struct PeerPutMessage
+{
+ /**
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * Processing options
+ */
+ uint32_t options GNUNET_PACKED;
+
+ /**
+ * Content type.
+ */
+ uint32_t type GNUNET_PACKED;
+
+ /**
+ * Hop count
+ */
+ uint32_t hop_count GNUNET_PACKED;
+
+ /**
+ * Replication level for this message
+ */
+ uint32_t desired_replication_level GNUNET_PACKED;
+
+ /**
+ * Length of the PUT path that follows (if tracked).
+ */
+ uint32_t put_path_length GNUNET_PACKED;
+
+ /**
+ * When does the content expire?
+ */
+ struct GNUNET_TIME_AbsoluteNBO expiration_time;
+
+ /**
+ * Bloomfilter (for peer identities) to stop circular routes
+ */
+ char bloomfilter[DHT_BLOOM_SIZE];
+
+ /**
+ * The key we are storing under.
+ */
+ struct GNUNET_HashCode key;
+
+ /* put path (if tracked) */
+
+ /* Payload */
+
+};
+
+
+/**
+ * P2P Result message
+ */
+struct PeerResultMessage
+{
+ /**
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * Content type.
+ */
+ uint32_t type GNUNET_PACKED;
+
+ /**
+ * Length of the PUT path that follows (if tracked).
+ */
+ uint32_t put_path_length GNUNET_PACKED;
+
+ /**
+ * Length of the GET path that follows (if tracked).
+ */
+ uint32_t get_path_length GNUNET_PACKED;
+
+ /**
+ * When does the content expire?
+ */
+ struct GNUNET_TIME_AbsoluteNBO expiration_time;
+
+ /**
+ * The key of the corresponding GET request.
+ */
+ struct GNUNET_HashCode key;
+
+ /* put path (if tracked) */
+
+ /* get path (if tracked) */
+
+ /* Payload */
+
+};
+
+
+/**
+ * P2P GET message
+ */
+struct PeerGetMessage
+{
+ /**
+ * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * Processing options
+ */
+ uint32_t options GNUNET_PACKED;
+
+ /**
+ * Desired content type.
+ */
+ uint32_t type GNUNET_PACKED;
+
+ /**
+ * Hop count
+ */
+ uint32_t hop_count GNUNET_PACKED;
+
+ /**
+ * Desired replication level for this request.
+ */
+ uint32_t desired_replication_level GNUNET_PACKED;
+
+ /**
+ * Size of the extended query.
+ */
+ uint32_t xquery_size;
+
+ /**
+ * Bloomfilter mutator.
+ */
+ uint32_t bf_mutator;
+
+ /**
+ * Bloomfilter (for peer identities) to stop circular routes
+ */
+ char bloomfilter[DHT_BLOOM_SIZE];
+
+ /**
+ * The key we are looking for.
+ */
+ struct GNUNET_HashCode key;
+
+ /* xquery */
+
+ /* result bloomfilter */
+
+};
+GNUNET_NETWORK_STRUCT_END
+
+/**
+ * Linked list of messages to send to a particular other peer.
+ */
+struct P2PPendingMessage
+{
+ /**
+ * Pointer to next item in the list
+ */
+ struct P2PPendingMessage *next;
+
+ /**
+ * Pointer to previous item in the list
+ */
+ struct P2PPendingMessage *prev;
+
+ /**
+ * Message importance level. FIXME: used? useful?
+ */
+ unsigned int importance;
+
+ /**
+ * When does this message time out?
+ */
+ struct GNUNET_TIME_Absolute timeout;
+
+ /**
+ * Actual message to be sent, allocated at the end of the struct:
+ * // msg = (cast) &pm[1];
+ * // memcpy (&pm[1], data, len);
+ */
+ const struct GNUNET_MessageHeader *msg;
+
+};
+
+
+/**
+ * Entry for a peer in a bucket.
+ */
+struct PeerInfo
+{
+ /**
+ * Next peer entry (DLL)
+ */
+ struct PeerInfo *next;
+
+ /**
+ * Prev peer entry (DLL)
+ */
+ struct PeerInfo *prev;
+
+ /**
+ * Count of outstanding messages for peer.
+ */
+ unsigned int pending_count;
+
+ /**
+ * Head of pending messages to be sent to this peer.
+ */
+ struct P2PPendingMessage *head;
+
+ /**
+ * Tail of pending messages to be sent to this peer.
+ */
+ struct P2PPendingMessage *tail;
+
+ /**
+ * Core handle for sending messages to this peer.
+ */
+ struct GNUNET_CORE_TransmitHandle *th;
+
+ /**
+ * Task for scheduling preference updates
+ */
+ GNUNET_SCHEDULER_TaskIdentifier preference_task;
+
+ /**
+ * What is the identity of the peer?
+ */
+ struct GNUNET_PeerIdentity id;
+
+#if 0
+ /**
+ * What is the average latency for replies received?
+ */
+ struct GNUNET_TIME_Relative latency;
+
+ /**
+ * Transport level distance to peer.
+ */
+ unsigned int distance;
+#endif
+
+};
+
+
+/**
+ * Peers are grouped into buckets.
+ */
+struct PeerBucket
+{
+ /**
+ * Head of DLL
+ */
+ struct PeerInfo *head;
+
+ /**
+ * Tail of DLL
+ */
+ struct PeerInfo *tail;
+
+ /**
+ * Number of peers in the bucket.
+ */
+ unsigned int peers_size;
+};
+
+
+/**
+ * Do we cache all results that we are routing in the local datacache?
+ */
+static int cache_results;
+
+/**
+ * Should routing details be logged to stderr (for debugging)?
+ */
+static int log_route_details_stderr;
+
+/**
+ * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
+ */
+static unsigned int closest_bucket;
+
+/**
+ * How many peers have we added since we sent out our last
+ * find peer request?
+ */
+static unsigned int newly_found_peers;
+
+/**
+ * Option for testing that disables the 'connect' function of the DHT.
+ */
+static int disable_try_connect;
+
+/**
+ * The buckets. Array of size MAX_BUCKET_SIZE. Offset 0 means 0 bits
matching.
+ */
+static struct PeerBucket k_buckets[MAX_BUCKETS];
+
+/**
+ * Hash map of all known peers, for easy removal from k_buckets on disconnect.
+ */
+static struct GNUNET_CONTAINER_MultiPeerMap *all_known_peers;
+
+/**
+ * Maximum size for each bucket.
+ */
+static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
+
+/**
+ * Task that sends FIND PEER requests.
+ */
+static GNUNET_SCHEDULER_TaskIdentifier find_peer_task;
+
+/**
+ * Identity of this peer.
+ */
+static struct GNUNET_PeerIdentity my_identity;
+
+/**
+ * Hash of the identity of this peer.
+ */
+static struct GNUNET_HashCode my_identity_hash;
+
+/**
+ * Handle to CORE.
+ */
+static struct GNUNET_CORE_Handle *core_api;
+
+/**
+ * Handle to ATS.
+ */
+static struct GNUNET_ATS_PerformanceHandle *atsAPI;
+
+
+
+/**
+ * Find the optimal bucket for this key.
+ *
+ * @param hc the hashcode to compare our identity to
+ * @return the proper bucket index, or GNUNET_SYSERR
+ * on error (same hashcode)
+ */
+static int
+find_bucket (const struct GNUNET_HashCode *hc)
+{
+ unsigned int bits;
+
+ bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, hc);
+ if (bits == MAX_BUCKETS)
+ {
+ /* How can all bits match? Got my own ID? */
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ return MAX_BUCKETS - bits - 1;
+}
+
+
+/**
+ * Let GNUnet core know that we like the given peer.
+ *
+ * @param cls the `struct PeerInfo` of the peer
+ * @param tc scheduler context.
+ */
+static void
+update_core_preference (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct PeerInfo *peer = cls;
+ uint64_t preference;
+ unsigned int matching;
+ int bucket;
+ struct GNUNET_HashCode phash;
+
+ peer->preference_task = GNUNET_SCHEDULER_NO_TASK;
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+ return;
+ GNUNET_CRYPTO_hash (&peer->id,
+ sizeof (struct GNUNET_PeerIdentity),
+ &phash);
+ matching =
+ GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
+ &phash);
+ if (matching >= 64)
+ matching = 63;
+ bucket = find_bucket (&phash);
+ if (bucket == GNUNET_SYSERR)
+ preference = 0;
+ else
+ {
+ GNUNET_assert (k_buckets[bucket].peers_size != 0);
+ preference = (1LL << matching) / k_buckets[bucket].peers_size;
+ }
+ if (preference == 0)
+ {
+ peer->preference_task =
+ GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
+ &update_core_preference, peer);
+ return;
+ }
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Preference updates given to
core"),
+ 1, GNUNET_NO);
+ GNUNET_ATS_performance_change_preference (atsAPI, &peer->id,
+ GNUNET_ATS_PREFERENCE_BANDWIDTH,
+ (double) preference,
GNUNET_ATS_PREFERENCE_END);
+ peer->preference_task =
+ GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
+ &update_core_preference, peer);
+
+
+}
+
+
+/**
+ * Closure for 'add_known_to_bloom'.
+ */
+struct BloomConstructorContext
+{
+ /**
+ * Bloom filter under construction.
+ */
+ struct GNUNET_CONTAINER_BloomFilter *bloom;
+
+ /**
+ * Mutator to use.
+ */
+ uint32_t bf_mutator;
+};
+
+
+/**
+ * Add each of the peers we already know to the bloom filter of
+ * the request so that we don't get duplicate HELLOs.
+ *
+ * @param cls the 'struct BloomConstructorContext'.
+ * @param key peer identity to add to the bloom filter
+ * @param value value the peer information (unused)
+ * @return #GNUNET_YES (we should continue to iterate)
+ */
+static int
+add_known_to_bloom (void *cls,
+ const struct GNUNET_PeerIdentity *key,
+ void *value)
+{
+ struct BloomConstructorContext *ctx = cls;
+ struct GNUNET_HashCode key_hash;
+ struct GNUNET_HashCode mh;
+
+ GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_PeerIdentity), &key_hash);
+ GNUNET_BLOCK_mingle_hash (&key_hash, ctx->bf_mutator, &mh);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding known peer (%s) to bloomfilter for FIND PEER with
mutation %u\n",
+ GNUNET_i2s (key), ctx->bf_mutator);
+ GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
+ return GNUNET_YES;
+}
+
+
+/**
+ * Task to send a find peer message for our own peer identifier
+ * so that we can find the closest peers in the network to ourselves
+ * and attempt to connect to them.
+ *
+ * @param cls closure for this task
+ * @param tc the context under which the task is running
+ */
+static void
+send_find_peer_message (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_TIME_Relative next_send_time;
+ struct BloomConstructorContext bcc;
+ struct GNUNET_CONTAINER_BloomFilter *peer_bf;
+
+ find_peer_task = GNUNET_SCHEDULER_NO_TASK;
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+ return;
+ if (newly_found_peers > bucket_size)
+ {
+ /* If we are finding many peers already, no need to send out our request
right now! */
+ find_peer_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
+ &send_find_peer_message, NULL);
+ newly_found_peers = 0;
+ return;
+ }
+ bcc.bf_mutator =
+ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
+ bcc.bloom =
+ GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ GNUNET_CONTAINER_multipeermap_iterate (all_known_peers, &add_known_to_bloom,
+ &bcc);
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# FIND PEER messages initiated"), 1,
+ GNUNET_NO);
+ peer_bf =
+ GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ // FIXME: pass priority!?
+ GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
+ GNUNET_DHT_RO_FIND_PEER,
+ FIND_PEER_REPLICATION_LEVEL, 0,
+ &my_identity_hash, NULL, 0, bcc.bloom,
+ bcc.bf_mutator, peer_bf);
+ GNUNET_CONTAINER_bloomfilter_free (peer_bf);
+ GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
+ /* schedule next round */
+ next_send_time.rel_value_us =
+ DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value_us +
+ GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
+ DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value_us /
+ (newly_found_peers + 1));
+ newly_found_peers = 0;
+ find_peer_task =
+ GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
+ NULL);
+}
+
+
+/**
+ * Method called whenever a peer connects.
+ *
+ * @param cls closure
+ * @param peer peer identity this notification is about
+ */
+static void
+handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
+{
+ struct PeerInfo *ret;
+ struct GNUNET_HashCode phash;
+ int peer_bucket;
+
+ /* Check for connect to self message */
+ if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
+ return;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Connected to %s\n",
+ GNUNET_i2s (peer));
+ if (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_contains (all_known_peers,
+ peer))
+ {
+ GNUNET_break (0);
+ return;
+ }
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), 1,
+ GNUNET_NO);
+ GNUNET_CRYPTO_hash (peer,
+ sizeof (struct GNUNET_PeerIdentity),
+ &phash);
+ peer_bucket = find_bucket (&phash);
+ GNUNET_assert ((peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS));
+ ret = GNUNET_malloc (sizeof (struct PeerInfo));
+#if 0
+ ret->latency = latency;
+ ret->distance = distance;
+#endif
+ ret->id = *peer;
+ GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head,
+ k_buckets[peer_bucket].tail, ret);
+ k_buckets[peer_bucket].peers_size++;
+ closest_bucket = GNUNET_MAX (closest_bucket, peer_bucket);
+ if ((peer_bucket > 0) && (k_buckets[peer_bucket].peers_size <= bucket_size))
+ {
+ ret->preference_task =
+ GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
+ newly_found_peers++;
+ }
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multipeermap_put (all_known_peers,
+ peer, ret,
+
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ if (1 == GNUNET_CONTAINER_multipeermap_size (all_known_peers) &&
+ (GNUNET_YES != disable_try_connect))
+ {
+ /* got a first connection, good time to start with FIND PEER requests... */
+ find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
+ }
+}
+
+
+/**
+ * Method called whenever a peer disconnects.
+ *
+ * @param cls closure
+ * @param peer peer identity this notification is about
+ */
+static void
+handle_core_disconnect (void *cls,
+ const struct GNUNET_PeerIdentity *peer)
+{
+ struct PeerInfo *to_remove;
+ int current_bucket;
+ struct P2PPendingMessage *pos;
+ unsigned int discarded;
+ struct GNUNET_HashCode phash;
+
+ /* Check for disconnect from self message */
+ if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
+ return;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnected %s\n",
+ GNUNET_i2s (peer));
+ to_remove =
+ GNUNET_CONTAINER_multipeermap_get (all_known_peers, peer);
+ if (NULL == to_remove)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), -1,
+ GNUNET_NO);
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_remove (all_known_peers,
+ peer,
+ to_remove));
+ if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task)
+ {
+ GNUNET_SCHEDULER_cancel (to_remove->preference_task);
+ to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ GNUNET_CRYPTO_hash (peer,
+ sizeof (struct GNUNET_PeerIdentity),
+ &phash);
+ current_bucket = find_bucket (&phash);
+ GNUNET_assert (current_bucket >= 0);
+ GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
+ k_buckets[current_bucket].tail, to_remove);
+ GNUNET_assert (k_buckets[current_bucket].peers_size > 0);
+ k_buckets[current_bucket].peers_size--;
+ while ((closest_bucket > 0) && (k_buckets[closest_bucket].peers_size == 0))
+ closest_bucket--;
+
+ if (to_remove->th != NULL)
+ {
+ GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
+ to_remove->th = NULL;
+ }
+ discarded = 0;
+ while (NULL != (pos = to_remove->head))
+ {
+ GNUNET_CONTAINER_DLL_remove (to_remove->head, to_remove->tail, pos);
+ discarded++;
+ GNUNET_free (pos);
+ }
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Queued messages discarded (peer
disconnected)"),
+ discarded, GNUNET_NO);
+ GNUNET_free (to_remove);
+}
+
+
+/**
+ * Called when core is ready to send a message we asked for
+ * out to the destination.
+ *
+ * @param cls the 'struct PeerInfo' of the target peer
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+static size_t
+core_transmit_notify (void *cls, size_t size, void *buf)
+{
+ struct PeerInfo *peer = cls;
+ char *cbuf = buf;
+ struct P2PPendingMessage *pending;
+ size_t off;
+ size_t msize;
+
+ peer->th = NULL;
+ while ((NULL != (pending = peer->head)) &&
+ (0 == GNUNET_TIME_absolute_get_remaining
(pending->timeout).rel_value_us))
+ {
+ peer->pending_count--;
+ GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
+ GNUNET_free (pending);
+ }
+ if (pending == NULL)
+ {
+ /* no messages pending */
+ return 0;
+ }
+ if (buf == NULL)
+ {
+ peer->th =
+ GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
+ pending->importance,
+ GNUNET_TIME_absolute_get_remaining
+ (pending->timeout), &peer->id,
+ ntohs (pending->msg->size),
+ &core_transmit_notify, peer);
+ GNUNET_break (NULL != peer->th);
+ return 0;
+ }
+ off = 0;
+ while ((NULL != (pending = peer->head)) &&
+ (size - off >= (msize = ntohs (pending->msg->size))))
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Bytes transmitted to other peers"), msize,
+ GNUNET_NO);
+ memcpy (&cbuf[off], pending->msg, msize);
+ off += msize;
+ peer->pending_count--;
+ GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
+ GNUNET_free (pending);
+ }
+ if (peer->head != NULL)
+ {
+ peer->th =
+ GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
+ pending->importance,
+ GNUNET_TIME_absolute_get_remaining
+ (pending->timeout), &peer->id,
msize,
+ &core_transmit_notify, peer);
+ GNUNET_break (NULL != peer->th);
+ }
+ return off;
+}
+
+
+/**
+ * Transmit all messages in the peer's message queue.
+ *
+ * @param peer message queue to process
+ */
+static void
+process_peer_queue (struct PeerInfo *peer)
+{
+ struct P2PPendingMessage *pending;
+
+ if (NULL == (pending = peer->head))
+ return;
+ if (NULL != peer->th)
+ return;
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Bytes of bandwidth requested from core"),
+ ntohs (pending->msg->size), GNUNET_NO);
+ peer->th =
+ GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
+ pending->importance,
+ GNUNET_TIME_absolute_get_remaining
+ (pending->timeout), &peer->id,
+ ntohs (pending->msg->size),
+ &core_transmit_notify, peer);
+ GNUNET_break (NULL != peer->th);
+}
+
+
+/**
+ * To how many peers should we (on average) forward the request to
+ * obtain the desired target_replication count (on average).
+ *
+ * @param hop_count number of hops the message has traversed
+ * @param target_replication the number of total paths desired
+ * @return Some number of peers to forward the message to
+ */
+static unsigned int
+get_forward_count (uint32_t hop_count, uint32_t target_replication)
+{
+ uint32_t random_value;
+ uint32_t forward_count;
+ float target_value;
+
+ if (hop_count > GDS_NSE_get () * 4.0)
+ {
+ /* forcefully terminate */
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# requests TTL-dropped"),
+ 1, GNUNET_NO);
+ return 0;
+ }
+ if (hop_count > GDS_NSE_get () * 2.0)
+ {
+ /* Once we have reached our ideal number of hops, only forward to 1 peer */
+ return 1;
+ }
+ /* bound by system-wide maximum */
+ target_replication =
+ GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL, target_replication);
+ target_value =
+ 1 + (target_replication - 1.0) / (GDS_NSE_get () +
+ ((float) (target_replication - 1.0) *
+ hop_count));
+ /* Set forward count to floor of target_value */
+ forward_count = (uint32_t) target_value;
+ /* Subtract forward_count (floor) from target_value (yields value between 0
and 1) */
+ target_value = target_value - forward_count;
+ random_value =
+ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
+ if (random_value < (target_value * UINT32_MAX))
+ forward_count++;
+ return forward_count;
+}
+
+
+/**
+ * Compute the distance between have and target as a 32-bit value.
+ * Differences in the lower bits must count stronger than differences
+ * in the higher bits.
+ *
+ * @param target
+ * @param have
+ * @return 0 if have==target, otherwise a number
+ * that is larger as the distance between
+ * the two hash codes increases
+ */
+static unsigned int
+get_distance (const struct GNUNET_HashCode *target,
+ const struct GNUNET_HashCode *have)
+{
+ unsigned int bucket;
+ unsigned int msb;
+ unsigned int lsb;
+ unsigned int i;
+
+ /* We have to represent the distance between two 2^9 (=512)-bit
+ * numbers as a 2^5 (=32)-bit number with "0" being used for the
+ * two numbers being identical; furthermore, we need to
+ * guarantee that a difference in the number of matching
+ * bits is always represented in the result.
+ *
+ * We use 2^32/2^9 numerical values to distinguish between
+ * hash codes that have the same LSB bit distance and
+ * use the highest 2^9 bits of the result to signify the
+ * number of (mis)matching LSB bits; if we have 0 matching
+ * and hence 512 mismatching LSB bits we return -1 (since
+ * 512 itself cannot be represented with 9 bits) */
+
+ /* first, calculate the most significant 9 bits of our
+ * result, aka the number of LSBs */
+ bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
+ /* bucket is now a value between 0 and 512 */
+ if (bucket == 512)
+ return 0; /* perfect match */
+ if (bucket == 0)
+ return (unsigned int) -1; /* LSB differs; use max (if we did the
bit-shifting
+ * below, we'd end up with max+1 (overflow)) */
+
+ /* calculate the most significant bits of the final result */
+ msb = (512 - bucket) << (32 - 9);
+ /* calculate the 32-9 least significant bits of the final result by
+ * looking at the differences in the 32-9 bits following the
+ * mismatching bit at 'bucket' */
+ lsb = 0;
+ for (i = bucket + 1;
+ (i < sizeof (struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9);
i++)
+ {
+ if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
+ GNUNET_CRYPTO_hash_get_bit (have, i))
+ lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
+ * last bit set will be 31 --
if
+ * i does not reach 512
first... */
+ }
+ return msb | lsb;
+}
+
+
+/**
+ * Check whether my identity is closer than any known peers. If a
+ * non-null bloomfilter is given, check if this is the closest peer
+ * that hasn't already been routed to.
+ *
+ * @param key hash code to check closeness to
+ * @param bloom bloomfilter, exclude these entries from the decision
+ * @return GNUNET_YES if node location is closest,
+ * GNUNET_NO otherwise.
+ */
+static int
+am_closest_peer (const struct GNUNET_HashCode *key,
+ const struct GNUNET_CONTAINER_BloomFilter *bloom)
+{
+ int bits;
+ int other_bits;
+ int bucket_num;
+ int count;
+ struct PeerInfo *pos;
+ struct GNUNET_HashCode phash;
+
+ if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode)))
+ return GNUNET_YES;
+ bucket_num = find_bucket (key);
+ GNUNET_assert (bucket_num >= 0);
+ bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, key);
+ pos = k_buckets[bucket_num].head;
+ count = 0;
+ while ((pos != NULL) && (count < bucket_size))
+ {
+ GNUNET_CRYPTO_hash (&pos->id,
+ sizeof (struct GNUNET_PeerIdentity),
+ &phash);
+ if ((bloom != NULL) &&
+ (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom, &phash)))
+ {
+ pos = pos->next;
+ continue; /* Skip already checked entries */
+ }
+ other_bits = GNUNET_CRYPTO_hash_matching_bits (&phash, key);
+ if (other_bits > bits)
+ return GNUNET_NO;
+ if (other_bits == bits) /* We match the same number of bits */
+ return GNUNET_YES;
+ pos = pos->next;
+ }
+ /* No peers closer, we are the closest! */
+ return GNUNET_YES;
+}
+
+
+/**
+ * Select a peer from the routing table that would be a good routing
+ * destination for sending a message for "key". The resulting peer
+ * must not be in the set of blocked peers.<p>
+ *
+ * Note that we should not ALWAYS select the closest peer to the
+ * target, peers further away from the target should be chosen with
+ * exponentially declining probability.
+ *
+ * FIXME: double-check that this is fine
+ *
+ *
+ * @param key the key we are selecting a peer to route to
+ * @param bloom a bloomfilter containing entries this request has seen already
+ * @param hops how many hops has this message traversed thus far
+ * @return Peer to route to, or NULL on error
+ */
+static struct PeerInfo *
+select_peer (const struct GNUNET_HashCode * key,
+ const struct GNUNET_CONTAINER_BloomFilter *bloom, uint32_t hops)
+{
+ unsigned int bc;
+ unsigned int count;
+ unsigned int selected;
+ struct PeerInfo *pos;
+ unsigned int dist;
+ unsigned int smallest_distance;
+ struct PeerInfo *chosen;
+ struct GNUNET_HashCode phash;
+
+ if (hops >= GDS_NSE_get ())
+ {
+ /* greedy selection (closest peer that is not in bloomfilter) */
+ smallest_distance = UINT_MAX;
+ chosen = NULL;
+ for (bc = 0; bc <= closest_bucket; bc++)
+ {
+ pos = k_buckets[bc].head;
+ count = 0;
+ while ((pos != NULL) && (count < bucket_size))
+ {
+ GNUNET_CRYPTO_hash (&pos->id,
+ sizeof (struct GNUNET_PeerIdentity),
+ &phash);
+ if ((bloom == NULL) ||
+ (GNUNET_NO ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom, &phash)))
+ {
+ dist = get_distance (key, &phash);
+ if (dist < smallest_distance)
+ {
+ chosen = pos;
+ smallest_distance = dist;
+ }
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Excluded peer `%s' due to BF match in greedy routing
for %s\n",
+ GNUNET_i2s (&pos->id), GNUNET_h2s (key));
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Peers excluded from routing due to
Bloomfilter"),
+ 1, GNUNET_NO);
+ dist = get_distance (key, &phash);
+ if (dist < smallest_distance)
+ {
+ chosen = NULL;
+ smallest_distance = dist;
+ }
+ }
+ count++;
+ pos = pos->next;
+ }
+ }
+ if (NULL == chosen)
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Peer selection failed"), 1,
+ GNUNET_NO);
+ return chosen;
+ }
+
+ /* select "random" peer */
+ /* count number of peers that are available and not filtered */
+ count = 0;
+ for (bc = 0; bc <= closest_bucket; bc++)
+ {
+ pos = k_buckets[bc].head;
+ while ((pos != NULL) && (count < bucket_size))
+ {
+ GNUNET_CRYPTO_hash (&pos->id,
+ sizeof (struct GNUNET_PeerIdentity),
+ &phash);
+ if ((bloom != NULL) &&
+ (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom, &phash)))
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Peers excluded from routing due to
Bloomfilter"),
+ 1, GNUNET_NO);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Excluded peer `%s' due to BF match in random routing for
%s\n",
+ GNUNET_i2s (&pos->id), GNUNET_h2s (key));
+ pos = pos->next;
+ continue; /* Ignore bloomfiltered peers */
+ }
+ count++;
+ pos = pos->next;
+ }
+ }
+ if (0 == count) /* No peers to select from! */
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Peer selection failed"), 1,
+ GNUNET_NO);
+ return NULL;
+ }
+ /* Now actually choose a peer */
+ selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
+ count = 0;
+ for (bc = 0; bc <= closest_bucket; bc++)
+ {
+ for (pos = k_buckets[bc].head; ((pos != NULL) && (count < bucket_size));
pos = pos->next)
+ {
+ GNUNET_CRYPTO_hash (&pos->id,
+ sizeof (struct GNUNET_PeerIdentity),
+ &phash);
+ if ((bloom != NULL) &&
+ (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom, &phash)))
+ {
+ continue; /* Ignore bloomfiltered peers */
+ }
+ if (0 == selected--)
+ return pos;
+ }
+ }
+ GNUNET_break (0);
+ return NULL;
+}
+
+
+/**
+ * Compute the set of peers that the given request should be
+ * forwarded to.
+ *
+ * @param key routing key
+ * @param bloom bloom filter excluding peers as targets, all selected
+ * peers will be added to the bloom filter
+ * @param hop_count number of hops the request has traversed so far
+ * @param target_replication desired number of replicas
+ * @param targets where to store an array of target peers (to be
+ * free'd by the caller)
+ * @return number of peers returned in 'targets'.
+ */
+static unsigned int
+get_target_peers (const struct GNUNET_HashCode *key,
+ struct GNUNET_CONTAINER_BloomFilter *bloom,
+ uint32_t hop_count, uint32_t target_replication,
+ struct PeerInfo ***targets)
+{
+ unsigned int ret;
+ unsigned int off;
+ struct PeerInfo **rtargets;
+ struct PeerInfo *nxt;
+ struct GNUNET_HashCode nhash;
+
+ GNUNET_assert (NULL != bloom);
+ ret = get_forward_count (hop_count, target_replication);
+ if (0 == ret)
+ {
+ *targets = NULL;
+ return 0;
+ }
+ rtargets = GNUNET_malloc (sizeof (struct PeerInfo *) * ret);
+ for (off = 0; off < ret; off++)
+ {
+ nxt = select_peer (key, bloom, hop_count);
+ if (NULL == nxt)
+ break;
+ rtargets[off] = nxt;
+ GNUNET_CRYPTO_hash (&nxt->id,
+ sizeof (struct GNUNET_PeerIdentity),
+ &nhash);
+ GNUNET_break (GNUNET_NO ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom,
+ &nhash));
+ GNUNET_CONTAINER_bloomfilter_add (bloom, &nhash);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Selected %u/%u peers at hop %u for %s (target was %u)\n", off,
+ GNUNET_CONTAINER_multipeermap_size (all_known_peers),
+ (unsigned int) hop_count, GNUNET_h2s (key), ret);
+ if (0 == off)
+ {
+ GNUNET_free (rtargets);
+ *targets = NULL;
+ return 0;
+ }
+ *targets = rtargets;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Forwarding query `%s' to %u peers (goal was %u peers)\n",
+ GNUNET_h2s (key),
+ off,
+ ret);
+ return off;
+}
+
+
+/**
+ * Perform a PUT operation. Forwards the given request to other
+ * peers. Does not store the data locally. Does not give the
+ * data to local clients. May do nothing if this is the only
+ * peer in the network (or if we are the closest peer in the
+ * network).
+ *
+ * @param type type of the block
+ * @param options routing options
+ * @param desired_replication_level desired replication count
+ * @param expiration_time when does the content expire
+ * @param hop_count how many hops has this message traversed so far
+ * @param bf Bloom filter of peers this PUT has already traversed
+ * @param key key for the content
+ * @param put_path_length number of entries in @a put_path
+ * @param put_path peers this request has traversed so far (if tracked)
+ * @param data payload to store
+ * @param data_size number of bytes in @a data
+ */
+void
+GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
+ enum GNUNET_DHT_RouteOption options,
+ uint32_t desired_replication_level,
+ struct GNUNET_TIME_Absolute expiration_time,
+ uint32_t hop_count,
+ struct GNUNET_CONTAINER_BloomFilter *bf,
+ const struct GNUNET_HashCode *key,
+ unsigned int put_path_length,
+ struct GNUNET_PeerIdentity *put_path,
+ const void *data, size_t data_size)
+{
+ unsigned int target_count;
+ unsigned int i;
+ struct PeerInfo **targets;
+ struct PeerInfo *target;
+ struct P2PPendingMessage *pending;
+ size_t msize;
+ struct PeerPutMessage *ppm;
+ struct GNUNET_PeerIdentity *pp;
+ struct GNUNET_HashCode thash;
+
+ GNUNET_assert (NULL != bf);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding myself (%s) to PUT bloomfilter for %s\n",
+ GNUNET_i2s (&my_identity), GNUNET_h2s (key));
+ GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity_hash);
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# PUT requests routed"),
+ 1, GNUNET_NO);
+ target_count =
+ get_target_peers (key, bf, hop_count, desired_replication_level,
+ &targets);
+ if (0 == target_count)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Routing PUT for %s terminates after %u hops at %s\n",
+ GNUNET_h2s (key), (unsigned int) hop_count,
+ GNUNET_i2s (&my_identity));
+ return;
+ }
+ msize =
+ put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
+ sizeof (struct PeerPutMessage);
+ if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ put_path_length = 0;
+ msize = data_size + sizeof (struct PeerPutMessage);
+ }
+ if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_break (0);
+ GNUNET_free (targets);
+ return;
+ }
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# PUT messages queued for transmission"),
+ target_count, GNUNET_NO);
+ for (i = 0; i < target_count; i++)
+ {
+ target = targets[i];
+ if (target->pending_count >= MAXIMUM_PENDING_PER_PEER)
+ {
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages
dropped due to full queue"),
+ 1, GNUNET_NO);
+ continue; /* skip */
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Routing PUT for %s after %u hops to %s\n", GNUNET_h2s (key),
+ (unsigned int) hop_count, GNUNET_i2s (&target->id));
+ pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
+ pending->importance = 0; /* FIXME */
+ pending->timeout = expiration_time;
+ ppm = (struct PeerPutMessage *) &pending[1];
+ pending->msg = &ppm->header;
+ ppm->header.size = htons (msize);
+ ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
+ ppm->options = htonl (options);
+ ppm->type = htonl (type);
+ ppm->hop_count = htonl (hop_count + 1);
+ ppm->desired_replication_level = htonl (desired_replication_level);
+ ppm->put_path_length = htonl (put_path_length);
+ ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
+ GNUNET_CRYPTO_hash (&target->id,
+ sizeof (struct GNUNET_PeerIdentity),
+ &thash);
+ GNUNET_break (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (bf,
+ &thash));
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
+ ppm->bloomfilter,
+ DHT_BLOOM_SIZE));
+ ppm->key = *key;
+ pp = (struct GNUNET_PeerIdentity *) &ppm[1];
+ memcpy (pp, put_path,
+ sizeof (struct GNUNET_PeerIdentity) * put_path_length);
+ memcpy (&pp[put_path_length], data, data_size);
+ GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending);
+ target->pending_count++;
+ process_peer_queue (target);
+ }
+ GNUNET_free (targets);
+}
+
+
+/**
+ * Perform a GET operation. Forwards the given request to other
+ * peers. Does not lookup the key locally. May do nothing if this is
+ * the only peer in the network (or if we are the closest peer in the
+ * network).
+ *
+ * @param type type of the block
+ * @param options routing options
+ * @param desired_replication_level desired replication count
+ * @param hop_count how many hops did this request traverse so far?
+ * @param key key for the content
+ * @param xquery extended query
+ * @param xquery_size number of bytes in @a xquery
+ * @param reply_bf bloomfilter to filter duplicates
+ * @param reply_bf_mutator mutator for @a reply_bf
+ * @param peer_bf filter for peers not to select (again)
+ */
+void
+GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
+ enum GNUNET_DHT_RouteOption options,
+ uint32_t desired_replication_level,
+ uint32_t hop_count, const struct GNUNET_HashCode *
key,
+ const void *xquery, size_t xquery_size,
+ const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
+ uint32_t reply_bf_mutator,
+ struct GNUNET_CONTAINER_BloomFilter *peer_bf)
+{
+ unsigned int target_count;
+ unsigned int i;
+ struct PeerInfo **targets;
+ struct PeerInfo *target;
+ struct P2PPendingMessage *pending;
+ size_t msize;
+ struct PeerGetMessage *pgm;
+ char *xq;
+ size_t reply_bf_size;
+ struct GNUNET_HashCode thash;
+
+ GNUNET_assert (NULL != peer_bf);
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# GET requests routed"),
+ 1, GNUNET_NO);
+ target_count =
+ get_target_peers (key, peer_bf, hop_count, desired_replication_level,
+ &targets);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding myself (%s) to GET bloomfilter for %s\n",
+ GNUNET_i2s (&my_identity), GNUNET_h2s (key));
+ GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity_hash);
+ if (0 == target_count)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Routing GET for %s terminates after %u hops at %s\n",
+ GNUNET_h2s (key), (unsigned int) hop_count,
+ GNUNET_i2s (&my_identity));
+ return;
+ }
+ reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
+ msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
+ if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_break (0);
+ GNUNET_free (targets);
+ return;
+ }
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# GET messages queued for transmission"),
+ target_count, GNUNET_NO);
+ /* forward request */
+ for (i = 0; i < target_count; i++)
+ {
+ target = targets[i];
+ if (target->pending_count >= MAXIMUM_PENDING_PER_PEER)
+ {
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages
dropped due to full queue"),
+ 1, GNUNET_NO);
+ continue; /* skip */
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Routing GET for %s after %u hops to %s\n", GNUNET_h2s (key),
+ (unsigned int) hop_count, GNUNET_i2s (&target->id));
+ pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
+ pending->importance = 0; /* FIXME */
+ pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
+ pgm = (struct PeerGetMessage *) &pending[1];
+ pending->msg = &pgm->header;
+ pgm->header.size = htons (msize);
+ pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
+ pgm->options = htonl (options);
+ pgm->type = htonl (type);
+ pgm->hop_count = htonl (hop_count + 1);
+ pgm->desired_replication_level = htonl (desired_replication_level);
+ pgm->xquery_size = htonl (xquery_size);
+ pgm->bf_mutator = reply_bf_mutator;
+ GNUNET_CRYPTO_hash (&target->id,
+ sizeof (struct GNUNET_PeerIdentity),
+ &thash);
+ GNUNET_break (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (peer_bf,
+ &thash));
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
+ pgm->bloomfilter,
+ DHT_BLOOM_SIZE));
+ pgm->key = *key;
+ xq = (char *) &pgm[1];
+ memcpy (xq, xquery, xquery_size);
+ if (NULL != reply_bf)
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
+ &xq
+ [xquery_size],
+
reply_bf_size));
+ GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending);
+ target->pending_count++;
+ process_peer_queue (target);
+ }
+ GNUNET_free (targets);
+}
+
+
+/**
+ * Handle a reply (route to origin). Only forwards the reply back to
+ * the given peer. Does not do local caching or forwarding to local
+ * clients.
+ *
+ * @param target neighbour that should receive the block (if still connected)
+ * @param type type of the block
+ * @param expiration_time when does the content expire
+ * @param key key for the content
+ * @param put_path_length number of entries in @a put_path
+ * @param put_path peers the original PUT traversed (if tracked)
+ * @param get_path_length number of entries in @a get_path
+ * @param get_path peers this reply has traversed so far (if tracked)
+ * @param data payload of the reply
+ * @param data_size number of bytes in @a data
+ */
+void
+GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
+ enum GNUNET_BLOCK_Type type,
+ struct GNUNET_TIME_Absolute expiration_time,
+ const struct GNUNET_HashCode * key,
+ unsigned int put_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *get_path,
+ const void *data, size_t data_size)
+{
+ struct PeerInfo *pi;
+ struct P2PPendingMessage *pending;
+ size_t msize;
+ struct PeerResultMessage *prm;
+ struct GNUNET_PeerIdentity *paths;
+
+ msize =
+ data_size + sizeof (struct PeerResultMessage) + (get_path_length +
+ put_path_length) *
+ sizeof (struct GNUNET_PeerIdentity);
+ if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
+ (get_path_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
+ (put_path_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
+ (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE))
+ {
+ GNUNET_break (0);
+ return;
+ }
+ pi = GNUNET_CONTAINER_multipeermap_get (all_known_peers, target);
+ if (NULL == pi)
+ {
+ /* peer disconnected in the meantime, drop reply */
+ return;
+ }
+ if (pi->pending_count >= MAXIMUM_PENDING_PER_PEER)
+ {
+ /* skip */
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped
due to full queue"),
+ 1, GNUNET_NO);
+ return;
+ }
+
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# RESULT messages queued for transmission"), 1,
+ GNUNET_NO);
+ pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
+ pending->importance = 0; /* FIXME */
+ pending->timeout = expiration_time;
+ prm = (struct PeerResultMessage *) &pending[1];
+ pending->msg = &prm->header;
+ prm->header.size = htons (msize);
+ prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
+ prm->type = htonl (type);
+ prm->put_path_length = htonl (put_path_length);
+ prm->get_path_length = htonl (get_path_length);
+ prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
+ prm->key = *key;
+ paths = (struct GNUNET_PeerIdentity *) &prm[1];
+ memcpy (paths, put_path,
+ put_path_length * sizeof (struct GNUNET_PeerIdentity));
+ memcpy (&paths[put_path_length], get_path,
+ get_path_length * sizeof (struct GNUNET_PeerIdentity));
+ memcpy (&paths[put_path_length + get_path_length], data, data_size);
+ GNUNET_CONTAINER_DLL_insert (pi->head, pi->tail, pending);
+ pi->pending_count++;
+ process_peer_queue (pi);
+}
+
+
+/**
+ * To be called on core init/fail.
+ *
+ * @param cls service closure
+ * @param identity the public identity of this peer
+ */
+static void
+core_init (void *cls,
+ const struct GNUNET_PeerIdentity *identity)
+{
+ my_identity = *identity;
+ GNUNET_CRYPTO_hash (identity,
+ sizeof (struct GNUNET_PeerIdentity),
+ &my_identity_hash);
+}
+
+
+/**
+ * Core handler for p2p put requests.
+ *
+ * @param cls closure
+ * @param peer sender of the request
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @return #GNUNET_OK to keep the connection open,
+ * #GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_dht_p2p_put (void *cls,
+ const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ const struct PeerPutMessage *put;
+ const struct GNUNET_PeerIdentity *put_path;
+ const void *payload;
+ uint32_t putlen;
+ uint16_t msize;
+ size_t payload_size;
+ enum GNUNET_DHT_RouteOption options;
+ struct GNUNET_CONTAINER_BloomFilter *bf;
+ struct GNUNET_HashCode test_key;
+ struct GNUNET_HashCode phash;
+
+ msize = ntohs (message->size);
+ if (msize < sizeof (struct PeerPutMessage))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ put = (const struct PeerPutMessage *) message;
+ putlen = ntohl (put->put_path_length);
+ if ((msize <
+ sizeof (struct PeerPutMessage) +
+ putlen * sizeof (struct GNUNET_PeerIdentity)) ||
+ (putlen >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P PUT requests received"), 1,
+ GNUNET_NO);
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P PUT bytes received"), msize,
+ GNUNET_NO);
+ put_path = (const struct GNUNET_PeerIdentity *) &put[1];
+ payload = &put_path[putlen];
+ options = ntohl (put->options);
+ payload_size =
+ msize - (sizeof (struct PeerPutMessage) +
+ putlen * sizeof (struct GNUNET_PeerIdentity));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PUT for `%s' from %s\n",
+ GNUNET_h2s (&put->key), GNUNET_i2s (peer));
+ GNUNET_CRYPTO_hash (peer, sizeof (struct GNUNET_PeerIdentity), &phash);
+ if (GNUNET_YES == log_route_details_stderr)
+ {
+ char *tmp;
+
+ tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
+ LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
+ "XDHT PUT %s: %s->%s (%u, %u=>%u)\n",
+ GNUNET_h2s (&put->key), GNUNET_i2s (peer), tmp,
+ ntohl(put->hop_count),
+ GNUNET_CRYPTO_hash_matching_bits (&phash, &put->key),
+ GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
&put->key)
+ );
+ GNUNET_free (tmp);
+ }
+ switch (GNUNET_BLOCK_get_key
+ (GDS_block_context, ntohl (put->type), payload, payload_size,
+ &test_key))
+ {
+ case GNUNET_YES:
+ if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode)))
+ {
+ char *put_s = GNUNET_strdup (GNUNET_h2s (&put->key));
+ GNUNET_break_op (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "PUT with key `%s' for block with key %s\n",
+ put_s, GNUNET_h2s (&test_key));
+ GNUNET_free (put_s);
+ return GNUNET_YES;
+ }
+ break;
+ case GNUNET_NO:
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ case GNUNET_SYSERR:
+ /* cannot verify, good luck */
+ break;
+ }
+ if (ntohl (put->type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes
*/
+ {
+ switch (GNUNET_BLOCK_evaluate (GDS_block_context,
+ ntohl (put->type),
+ NULL, /* query */
+ NULL, 0, /* bloom filer */
+ NULL, 0, /* xquery */
+ payload, payload_size))
+ {
+ case GNUNET_BLOCK_EVALUATION_OK_MORE:
+ case GNUNET_BLOCK_EVALUATION_OK_LAST:
+ break;
+
+ case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
+ case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
+ case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
+ case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
+ case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
+ case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
+ default:
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ }
+
+ bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter, DHT_BLOOM_SIZE,
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ GNUNET_break_op (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (bf, &phash));
+ {
+ struct GNUNET_PeerIdentity pp[putlen + 1];
+
+ /* extend 'put path' by sender */
+ if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
+ {
+ memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
+ pp[putlen] = *peer;
+ putlen++;
+ }
+ else
+ putlen = 0;
+
+ /* give to local clients */
+ GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
+ &put->key, 0, NULL, putlen, pp, ntohl
(put->type),
+ payload_size, payload);
+ /* store locally */
+ if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
+ (am_closest_peer (&put->key, bf)))
+ GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh
+ (put->expiration_time), &put->key, putlen, pp,
+ ntohl (put->type), payload_size, payload);
+ /* route to other peers */
+ GDS_NEIGHBOURS_handle_put (ntohl (put->type), options,
+ ntohl (put->desired_replication_level),
+ GNUNET_TIME_absolute_ntoh
(put->expiration_time),
+ ntohl (put->hop_count), bf, &put->key, putlen,
+ pp, payload, payload_size);
+ /* notify monitoring clients */
+ GDS_CLIENTS_process_put (options,
+ ntohl (put->type),
+ ntohl (put->hop_count),
+ ntohl (put->desired_replication_level),
+ putlen, pp,
+ GNUNET_TIME_absolute_ntoh (put->expiration_time),
+ &put->key,
+ payload,
+ payload_size);
+ }
+ GNUNET_CONTAINER_bloomfilter_free (bf);
+ return GNUNET_YES;
+}
+
+
+/**
+ * We have received a FIND PEER request. Send matching
+ * HELLOs back.
+ *
+ * @param sender sender of the FIND PEER request
+ * @param key peers close to this key are desired
+ * @param bf peers matching this bf are excluded
+ * @param bf_mutator mutator for bf
+ */
+static void
+handle_find_peer (const struct GNUNET_PeerIdentity *sender,
+ const struct GNUNET_HashCode * key,
+ struct GNUNET_CONTAINER_BloomFilter *bf, uint32_t bf_mutator)
+{
+ int bucket_idx;
+ struct PeerBucket *bucket;
+ struct PeerInfo *peer;
+ unsigned int choice;
+ struct GNUNET_HashCode phash;
+ struct GNUNET_HashCode mhash;
+ const struct GNUNET_HELLO_Message *hello;
+
+ /* first, check about our own HELLO */
+ if (NULL != GDS_my_hello)
+ {
+ GNUNET_BLOCK_mingle_hash (&my_identity_hash, bf_mutator, &mhash);
+ if ((NULL == bf) ||
+ (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)))
+ {
+ GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO,
+ GNUNET_TIME_relative_to_absolute
+ (hello_expiration),
+ key, 0, NULL, 0, NULL, GDS_my_hello,
+ GNUNET_HELLO_size ((const struct
+ GNUNET_HELLO_Message *)
+ GDS_my_hello));
+ }
+ else
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# FIND PEER requests ignored due to
Bloomfilter"),
+ 1, GNUNET_NO);
+ }
+ }
+ else
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# FIND PEER requests ignored due to lack of
HELLO"),
+ 1, GNUNET_NO);
+ }
+
+ /* then, also consider sending a random HELLO from the closest bucket */
+ if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode)))
+ bucket_idx = closest_bucket;
+ else
+ bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key));
+ if (bucket_idx == GNUNET_SYSERR)
+ return;
+ bucket = &k_buckets[bucket_idx];
+ if (bucket->peers_size == 0)
+ return;
+ choice =
+ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
bucket->peers_size);
+ peer = bucket->head;
+ while (choice > 0)
+ {
+ GNUNET_assert (NULL != peer);
+ peer = peer->next;
+ choice--;
+ }
+ choice = bucket->peers_size;
+ do
+ {
+ peer = peer->next;
+ if (choice-- == 0)
+ return; /* no non-masked peer available */
+ if (peer == NULL)
+ peer = bucket->head;
+ GNUNET_CRYPTO_hash (&peer->id, sizeof (struct GNUNET_PeerIdentity),
&phash);
+ GNUNET_BLOCK_mingle_hash (&phash, bf_mutator, &mhash);
+ hello = GDS_HELLO_get (&peer->id);
+ }
+ while ((hello == NULL) ||
+ (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)));
+ GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO,
+ GNUNET_TIME_relative_to_absolute
+ (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
key,
+ 0, NULL, 0, NULL, hello,
+ GNUNET_HELLO_size (hello));
+}
+
+
+/**
+ * Core handler for p2p get requests.
+ *
+ * @param cls closure
+ * @param peer sender of the request
+ * @param message message
+ * @return #GNUNET_OK to keep the connection open,
+ * #GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct PeerGetMessage *get;
+ uint32_t xquery_size;
+ size_t reply_bf_size;
+ uint16_t msize;
+ enum GNUNET_BLOCK_Type type;
+ enum GNUNET_DHT_RouteOption options;
+ enum GNUNET_BLOCK_EvaluationResult eval;
+ struct GNUNET_CONTAINER_BloomFilter *reply_bf;
+ struct GNUNET_CONTAINER_BloomFilter *peer_bf;
+ const char *xquery;
+ struct GNUNET_HashCode phash;
+
+ GNUNET_break (0 !=
+ memcmp (peer, &my_identity,
+ sizeof (struct GNUNET_PeerIdentity)));
+ /* parse and validate message */
+ msize = ntohs (message->size);
+ if (msize < sizeof (struct PeerGetMessage))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ get = (struct PeerGetMessage *) message;
+ xquery_size = ntohl (get->xquery_size);
+ if (msize < sizeof (struct PeerGetMessage) + xquery_size)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
+ type = ntohl (get->type);
+ options = ntohl (get->options);
+ xquery = (const char *) &get[1];
+ reply_bf = NULL;
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P GET requests received"), 1,
+ GNUNET_NO);
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P GET bytes received"), msize,
+ GNUNET_NO);
+ GNUNET_CRYPTO_hash (peer, sizeof (struct GNUNET_PeerIdentity), &phash);
+ if (GNUNET_YES == log_route_details_stderr)
+ {
+ char *tmp;
+
+ tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
+ LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
+ "XDHT GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n",
+ GNUNET_h2s (&get->key), GNUNET_i2s (peer), tmp,
+ ntohl(get->hop_count),
+ GNUNET_CRYPTO_hash_matching_bits (&phash, &get->key),
+ GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
&get->key),
+ ntohl(get->xquery_size), xquery
+ );
+ GNUNET_free (tmp);
+ }
+
+ if (reply_bf_size > 0)
+ reply_bf =
+ GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size], reply_bf_size,
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ eval =
+ GNUNET_BLOCK_evaluate (GDS_block_context, type, &get->key, &reply_bf,
+ get->bf_mutator, xquery, xquery_size, NULL, 0);
+ if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
+ {
+ /* request invalid or block type not supported */
+ GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
+ if (NULL != reply_bf)
+ GNUNET_CONTAINER_bloomfilter_free (reply_bf);
+ return GNUNET_YES;
+ }
+ peer_bf =
+ GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, DHT_BLOOM_SIZE,
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ GNUNET_break_op (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (peer_bf,
+ &phash));
+ /* remember request for routing replies */
+ GDS_ROUTING_add (peer, type, options, &get->key, xquery, xquery_size,
+ reply_bf, get->bf_mutator);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GET for %s at %s after %u hops\n",
+ GNUNET_h2s (&get->key), GNUNET_i2s (&my_identity),
+ (unsigned int) ntohl (get->hop_count));
+ /* local lookup (this may update the reply_bf) */
+ if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
+ (am_closest_peer (&get->key, peer_bf)))
+ {
+ if ((0 != (options & GNUNET_DHT_RO_FIND_PEER)))
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# P2P FIND PEER requests processed"), 1,
+ GNUNET_NO);
+ handle_find_peer (peer, &get->key, reply_bf, get->bf_mutator);
+ }
+ else
+ {
+ eval =
+ GDS_DATACACHE_handle_get (&get->key, type, xquery, xquery_size,
+ &reply_bf, get->bf_mutator);
+ }
+ }
+ else
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P GET requests ONLY routed"),
+ 1, GNUNET_NO);
+ }
+
+ GDS_CLIENTS_process_get (options,
+ type,
+ ntohl(get->hop_count),
+ ntohl(get->desired_replication_level),
+ 0, NULL,
+ &get->key);
+
+ /* P2P forwarding */
+ if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
+ GDS_NEIGHBOURS_handle_get (type, options,
+ ntohl (get->desired_replication_level),
+ ntohl (get->hop_count), &get->key, xquery,
+ xquery_size, reply_bf, get->bf_mutator,
peer_bf);
+ /* clean up */
+ if (NULL != reply_bf)
+ GNUNET_CONTAINER_bloomfilter_free (reply_bf);
+ GNUNET_CONTAINER_bloomfilter_free (peer_bf);
+ return GNUNET_YES;
+}
+
+
+/**
+ * Core handler for p2p result messages.
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @return #GNUNET_YES (do not cut p2p connection)
+ */
+static int
+handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message)
+{
+ const struct PeerResultMessage *prm;
+ const struct GNUNET_PeerIdentity *put_path;
+ const struct GNUNET_PeerIdentity *get_path;
+ const void *data;
+ uint32_t get_path_length;
+ uint32_t put_path_length;
+ uint16_t msize;
+ size_t data_size;
+ enum GNUNET_BLOCK_Type type;
+
+ /* parse and validate message */
+ msize = ntohs (message->size);
+ if (msize < sizeof (struct PeerResultMessage))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ prm = (struct PeerResultMessage *) message;
+ put_path_length = ntohl (prm->put_path_length);
+ get_path_length = ntohl (prm->get_path_length);
+ if ((msize <
+ sizeof (struct PeerResultMessage) + (get_path_length +
+ put_path_length) *
+ sizeof (struct GNUNET_PeerIdentity)) ||
+ (get_path_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
+ (put_path_length >
+ GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ put_path = (const struct GNUNET_PeerIdentity *) &prm[1];
+ get_path = &put_path[put_path_length];
+ type = ntohl (prm->type);
+ data = (const void *) &get_path[get_path_length];
+ data_size =
+ msize - (sizeof (struct PeerResultMessage) +
+ (get_path_length +
+ put_path_length) * sizeof (struct GNUNET_PeerIdentity));
+ GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P RESULTS received"),
+ 1, GNUNET_NO);
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P RESULT bytes received"),
+ msize, GNUNET_NO);
+ if (GNUNET_YES == log_route_details_stderr)
+ {
+ char *tmp;
+
+ tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
+ LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "XDHT RESULT %s: %s->%s (%u)\n",
+ GNUNET_h2s (&prm->key), GNUNET_i2s (peer), tmp,
+ get_path_length + 1);
+ GNUNET_free (tmp);
+ }
+ /* if we got a HELLO, consider it for our own routing table */
+ if (type == GNUNET_BLOCK_TYPE_DHT_HELLO)
+ {
+ const struct GNUNET_MessageHeader *h;
+ struct GNUNET_PeerIdentity pid;
+ int bucket;
+
+ /* Should be a HELLO, validate and consider using it! */
+ if (data_size < sizeof (struct GNUNET_MessageHeader))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ h = data;
+ if (data_size != ntohs (h->size))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ if (GNUNET_OK !=
+ GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h, &pid))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_YES;
+ }
+ if ((GNUNET_YES != disable_try_connect) &&
+ 0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
+ {
+ struct GNUNET_HashCode pid_hash;
+
+ GNUNET_CRYPTO_hash (&pid, sizeof (struct GNUNET_PeerIdentity),
&pid_hash);
+ bucket = find_bucket (&pid_hash);
+ if ((bucket >= 0) &&
+ (k_buckets[bucket].peers_size < bucket_size) &&
+ (NULL != GDS_transport_handle))
+ {
+ GNUNET_TRANSPORT_offer_hello (GDS_transport_handle, h, NULL, NULL);
+ GNUNET_TRANSPORT_try_connect (GDS_transport_handle, &pid, NULL, NULL);
/*FIXME TRY_CONNECT change */
+ }
+ }
+ }
+
+ /* append 'peer' to 'get_path' */
+ {
+ struct GNUNET_PeerIdentity xget_path[get_path_length + 1];
+
+ memcpy (xget_path, get_path,
+ get_path_length * sizeof (struct GNUNET_PeerIdentity));
+ xget_path[get_path_length] = *peer;
+ get_path_length++;
+
+ /* forward to local clients */
+ GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
+ &prm->key, get_path_length, xget_path,
+ put_path_length, put_path, type, data_size,
data);
+ GDS_CLIENTS_process_get_resp (type,
+ xget_path, get_path_length,
+ put_path, put_path_length,
+ GNUNET_TIME_absolute_ntoh (
+ prm->expiration_time),
+ &prm->key,
+ data,
+ data_size);
+ if (GNUNET_YES == cache_results)
+ {
+ struct GNUNET_PeerIdentity xput_path[get_path_length + 1 +
put_path_length];
+
+ memcpy (xput_path, put_path, put_path_length * sizeof (struct
GNUNET_PeerIdentity));
+ memcpy (&xput_path[put_path_length],
+ xget_path,
+ get_path_length * sizeof (struct GNUNET_PeerIdentity));
+
+ GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh
(prm->expiration_time),
+ &prm->key,
+ get_path_length + put_path_length, xput_path,
+ type, data_size, data);
+ }
+ /* forward to other peers */
+ GDS_ROUTING_process (type, GNUNET_TIME_absolute_ntoh
(prm->expiration_time),
+ &prm->key, put_path_length, put_path, get_path_length,
+ xget_path, data, data_size);
+ }
+
+ return GNUNET_YES;
+}
+
+
+/**
+ * Initialize neighbours subsystem.
+ *
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GDS_NEIGHBOURS_init ()
+{
+ static struct GNUNET_CORE_MessageHandler core_handlers[] = {
+ {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
+ {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
+ {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0},
+ {NULL, 0, 0}
+ };
+ unsigned long long temp_config_num;
+
+ disable_try_connect
+ = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg, "DHT",
"DISABLE_TRY_CONNECT");
+ if (GNUNET_OK ==
+ GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size",
+ &temp_config_num))
+ bucket_size = (unsigned int) temp_config_num;
+ cache_results
+ = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg, "DHT", "CACHE_RESULTS");
+
+ log_route_details_stderr =
+ (NULL != getenv("GNUNET_DHT_ROUTE_DEBUG")) ? GNUNET_YES : GNUNET_NO;
+ atsAPI = GNUNET_ATS_performance_init (GDS_cfg, NULL, NULL);
+ core_api =
+ GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
+ &handle_core_disconnect, NULL, GNUNET_NO, NULL,
+ GNUNET_NO, core_handlers);
+ if (core_api == NULL)
+ return GNUNET_SYSERR;
+ all_known_peers = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Shutdown neighbours subsystem.
+ */
+void
+GDS_NEIGHBOURS_done ()
+{
+ if (NULL == core_api)
+ return;
+ GNUNET_CORE_disconnect (core_api);
+ core_api = NULL;
+ GNUNET_ATS_performance_done (atsAPI);
+ atsAPI = NULL;
+ GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (all_known_peers));
+ GNUNET_CONTAINER_multipeermap_destroy (all_known_peers);
+ all_known_peers = NULL;
+ if (GNUNET_SCHEDULER_NO_TASK != find_peer_task)
+ {
+ GNUNET_SCHEDULER_cancel (find_peer_task);
+ find_peer_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+}
+
+/**
+ * Get the ID of the local node.
+ *
+ * @return identity of the local node
+ */
+struct GNUNET_PeerIdentity *
+GDS_NEIGHBOURS_get_id ()
+{
+ return &my_identity;
+}
+
+
+/* end of gnunet-service-dht_neighbours.c */
Copied: gnunet/src/dht/gnunet-service-xdht_neighbours.h (from rev 31026,
gnunet/src/dht/gnunet-service-dht_neighbours.h)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_neighbours.h
(rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_neighbours.h 2013-12-05 10:25:18 UTC
(rev 31062)
@@ -0,0 +1,147 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_neighbours.h
+ * @brief GNUnet DHT routing code
+ * @author Christian Grothoff
+ * @author Nathan Evans
+ */
+#ifndef GNUNET_SERVICE_DHT_NEIGHBOURS_H
+#define GNUNET_SERVICE_DHT_NEIGHBOURS_H
+
+#include "gnunet_util_lib.h"
+#include "gnunet_block_lib.h"
+#include "gnunet_dht_service.h"
+
+/**
+ * Perform a PUT operation. Forwards the given request to other
+ * peers. Does not store the data locally. Does not give the
+ * data to local clients. May do nothing if this is the only
+ * peer in the network (or if we are the closest peer in the
+ * network).
+ *
+ * @param type type of the block
+ * @param options routing options
+ * @param desired_replication_level desired replication level
+ * @param expiration_time when does the content expire
+ * @param hop_count how many hops has this message traversed so far
+ * @param bf Bloom filter of peers this PUT has already traversed
+ * @param key key for the content
+ * @param put_path_length number of entries in put_path
+ * @param put_path peers this request has traversed so far (if tracked)
+ * @param data payload to store
+ * @param data_size number of bytes in data
+ */
+void
+GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
+ enum GNUNET_DHT_RouteOption options,
+ uint32_t desired_replication_level,
+ struct GNUNET_TIME_Absolute expiration_time,
+ uint32_t hop_count,
+ struct GNUNET_CONTAINER_BloomFilter *bf,
+ const struct GNUNET_HashCode * key,
+ unsigned int put_path_length,
+ struct GNUNET_PeerIdentity *put_path,
+ const void *data, size_t data_size);
+
+
+/**
+ * Perform a GET operation. Forwards the given request to other
+ * peers. Does not lookup the key locally. May do nothing if this is
+ * the only peer in the network (or if we are the closest peer in the
+ * network).
+ *
+ * @param type type of the block
+ * @param options routing options
+ * @param desired_replication_level desired replication count
+ * @param hop_count how many hops did this request traverse so far?
+ * @param key key for the content
+ * @param xquery extended query
+ * @param xquery_size number of bytes in xquery
+ * @param reply_bf bloomfilter to filter duplicates
+ * @param reply_bf_mutator mutator for reply_bf
+ * @param peer_bf filter for peers not to select (again, updated)
+ */
+void
+GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
+ enum GNUNET_DHT_RouteOption options,
+ uint32_t desired_replication_level,
+ uint32_t hop_count, const struct GNUNET_HashCode *
key,
+ const void *xquery, size_t xquery_size,
+ const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
+ uint32_t reply_bf_mutator,
+ struct GNUNET_CONTAINER_BloomFilter *peer_bf);
+
+
+/**
+ * Handle a reply (route to origin). Only forwards the reply back to
+ * other peers waiting for it. Does not do local caching or
+ * forwarding to local clients.
+ *
+ * @param target neighbour that should receive the block (if still connected)
+ * @param type type of the block
+ * @param expiration_time when does the content expire
+ * @param key key for the content
+ * @param put_path_length number of entries in put_path
+ * @param put_path peers the original PUT traversed (if tracked)
+ * @param get_path_length number of entries in put_path
+ * @param get_path peers this reply has traversed so far (if tracked)
+ * @param data payload of the reply
+ * @param data_size number of bytes in data
+ */
+void
+GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
+ enum GNUNET_BLOCK_Type type,
+ struct GNUNET_TIME_Absolute expiration_time,
+ const struct GNUNET_HashCode * key,
+ unsigned int put_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *get_path,
+ const void *data, size_t data_size);
+
+
+/**
+ * Initialize neighbours subsystem.
+ *
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GDS_NEIGHBOURS_init (void);
+
+
+/**
+ * Shutdown neighbours subsystem.
+ */
+void
+GDS_NEIGHBOURS_done (void);
+
+
+/**
+ * Get the ID of the local node.
+ *
+ * @return identity of the local node
+ */
+struct GNUNET_PeerIdentity *
+GDS_NEIGHBOURS_get_id ();
+
+
+#endif
Copied: gnunet/src/dht/gnunet-service-xdht_nse.c (from rev 31026,
gnunet/src/dht/gnunet-service-dht_nse.c)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_nse.c (rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_nse.c 2013-12-05 10:25:18 UTC (rev
31062)
@@ -0,0 +1,116 @@
+/*
+ This file is part of GNUnet.
+ (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_nse.c
+ * @brief GNUnet DHT integration with NSE
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "gnunet_nse_service.h"
+#include "gnunet-service-dht.h"
+#include "gnunet-service-dht_nse.h"
+
+/**
+ * log of the current network size estimate, used as the point where
+ * we switch between random and deterministic routing. Default
+ * value of 4.0 is used if NSE module is not available (i.e. not
+ * configured).
+ */
+static double log_of_network_size_estimate = 4.0;
+
+/**
+ * Network size estimation handle.
+ */
+static struct GNUNET_NSE_Handle *nse;
+
+
+/**
+ * Callback that is called when network size estimate is updated.
+ *
+ * @param cls closure
+ * @param timestamp time when the estimate was received from the server (or
created by the server)
+ * @param logestimate the log(Base 2) value of the current network size
estimate
+ * @param std_dev standard deviation for the estimate
+ *
+ */
+static void
+update_network_size_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
+ double logestimate, double std_dev)
+{
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Network size estimates received"),
+ 1, GNUNET_NO);
+ /* do not allow estimates < 0.5 */
+ log_of_network_size_estimate = GNUNET_MAX (0.5, logestimate);
+}
+
+
+/**
+ * Return the log of the current network size estimate.
+ *
+ * @return log of NSE
+ */
+double
+GDS_NSE_get ()
+{
+ return log_of_network_size_estimate;
+}
+
+
+/**
+ * Initialize NSE subsystem.
+ */
+void
+GDS_NSE_init ()
+{
+ unsigned long long hops;
+
+ if ( (GNUNET_YES ==
+ GNUNET_CONFIGURATION_have_value (GDS_cfg,
+ "dht",
+ "FORCE_NSE")) &&
+ (GNUNET_OK ==
+ GNUNET_CONFIGURATION_get_value_number (GDS_cfg,
+ "dht",
+ "FORCE_NSE",
+ &hops)) )
+ {
+ log_of_network_size_estimate = (double) hops;
+ return;
+ }
+ nse = GNUNET_NSE_connect (GDS_cfg, &update_network_size_estimate, NULL);
+}
+
+
+/**
+ * Shutdown NSE subsystem.
+ */
+void
+GDS_NSE_done ()
+{
+ if (NULL != nse)
+ {
+ GNUNET_NSE_disconnect (nse);
+ nse = NULL;
+ }
+}
+
+/* end of gnunet-service-dht_nse.c */
Copied: gnunet/src/dht/gnunet-service-xdht_nse.h (from rev 31026,
gnunet/src/dht/gnunet-service-dht_nse.h)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_nse.h (rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_nse.h 2013-12-05 10:25:18 UTC (rev
31062)
@@ -0,0 +1,52 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_nse.h
+ * @brief GNUnet DHT integration with NSE
+ * @author Christian Grothoff
+ */
+#ifndef GNUNET_SERVICE_DHT_NSE_H
+#define GNUNET_SERVICE_DHT_NSE_H
+
+
+/**
+ * Return the log of the current network size estimate.
+ *
+ * @return log of NSE
+ */
+double
+GDS_NSE_get (void);
+
+
+/**
+ * Initialize NSE subsystem.
+ */
+void
+GDS_NSE_init (void);
+
+
+/**
+ * Shutdown NSE subsystem.
+ */
+void
+GDS_NSE_done (void);
+
+#endif
Copied: gnunet/src/dht/gnunet-service-xdht_routing.c (from rev 31026,
gnunet/src/dht/gnunet-service-dht_routing.c)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_routing.c
(rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_routing.c 2013-12-05 10:25:18 UTC
(rev 31062)
@@ -0,0 +1,458 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_routing.c
+ * @brief GNUnet DHT tracking of requests for routing replies
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "gnunet-service-dht_neighbours.h"
+#include "gnunet-service-dht_routing.h"
+#include "gnunet-service-dht.h"
+
+
+/**
+ * Number of requests we track at most (for routing replies).
+ */
+#define DHT_MAX_RECENT (1024 * 16)
+
+
+/**
+ * Information we keep about all recent GET requests
+ * so that we can route replies.
+ */
+struct RecentRequest
+{
+
+ /**
+ * The peer this request was received from.
+ */
+ struct GNUNET_PeerIdentity peer;
+
+ /**
+ * Key of this request.
+ */
+ struct GNUNET_HashCode key;
+
+ /**
+ * Position of this node in the min heap.
+ */
+ struct GNUNET_CONTAINER_HeapNode *heap_node;
+
+ /**
+ * Bloomfilter for replies to drop.
+ */
+ struct GNUNET_CONTAINER_BloomFilter *reply_bf;
+
+ /**
+ * Type of the requested block.
+ */
+ enum GNUNET_BLOCK_Type type;
+
+ /**
+ * extended query (see gnunet_block_lib.h). Allocated at the
+ * end of this struct.
+ */
+ const void *xquery;
+
+ /**
+ * Number of bytes in xquery.
+ */
+ size_t xquery_size;
+
+ /**
+ * Mutator value for the reply_bf, see gnunet_block_lib.h
+ */
+ uint32_t reply_bf_mutator;
+
+ /**
+ * Request options.
+ */
+ enum GNUNET_DHT_RouteOption options;
+
+};
+
+
+/**
+ * Recent requests by time inserted.
+ */
+static struct GNUNET_CONTAINER_Heap *recent_heap;
+
+/**
+ * Recently seen requests by key.
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *recent_map;
+
+
+/**
+ * Closure for the 'process' function.
+ */
+struct ProcessContext
+{
+ /**
+ * Path of the original PUT
+ */
+ const struct GNUNET_PeerIdentity *put_path;
+
+ /**
+ * Path of the reply.
+ */
+ const struct GNUNET_PeerIdentity *get_path;
+
+ /**
+ * Payload of the reply.
+ */
+ const void *data;
+
+ /**
+ * Expiration time of the result.
+ */
+ struct GNUNET_TIME_Absolute expiration_time;
+
+ /**
+ * Number of entries in 'put_path'.
+ */
+ unsigned int put_path_length;
+
+ /**
+ * Number of entries in 'get_path'.
+ */
+ unsigned int get_path_length;
+
+ /**
+ * Number of bytes in 'data'.
+ */
+ size_t data_size;
+
+ /**
+ * Type of the reply.
+ */
+ enum GNUNET_BLOCK_Type type;
+
+};
+
+
+/**
+ * Forward the result to the given peer if it matches the request.
+ *
+ * @param cls the `struct ProcessContext` with the result
+ * @param key the query
+ * @param value the `struct RecentRequest` with the request
+ * @return #GNUNET_OK (continue to iterate),
+ * #GNUNET_SYSERR if the result is malformed or type unsupported
+ */
+static int
+process (void *cls, const struct GNUNET_HashCode * key, void *value)
+{
+ struct ProcessContext *pc = cls;
+ struct RecentRequest *rr = value;
+ enum GNUNET_BLOCK_EvaluationResult eval;
+ unsigned int gpl;
+ unsigned int ppl;
+ struct GNUNET_HashCode hc;
+ const struct GNUNET_HashCode *eval_key;
+
+ if ((rr->type != GNUNET_BLOCK_TYPE_ANY) && (rr->type != pc->type))
+ return GNUNET_OK; /* type missmatch */
+
+ if (0 != (rr->options & GNUNET_DHT_RO_RECORD_ROUTE))
+ {
+ gpl = pc->get_path_length;
+ ppl = pc->put_path_length;
+ }
+ else
+ {
+ gpl = 0;
+ ppl = 0;
+ }
+ if ((0 != (rr->options & GNUNET_DHT_RO_FIND_PEER)) &&
+ (pc->type == GNUNET_BLOCK_TYPE_DHT_HELLO))
+ {
+ /* key may not match HELLO, which is OK since
+ * the search is approximate. Still, the evaluation
+ * would fail since the match is not exact. So
+ * we fake it by changing the key to the actual PID ... */
+ GNUNET_BLOCK_get_key (GDS_block_context, GNUNET_BLOCK_TYPE_DHT_HELLO,
+ pc->data, pc->data_size, &hc);
+ eval_key = &hc;
+ }
+ else
+ {
+ eval_key = key;
+ }
+ eval =
+ GNUNET_BLOCK_evaluate (GDS_block_context, pc->type, eval_key,
+ &rr->reply_bf, rr->reply_bf_mutator, rr->xquery,
+ rr->xquery_size, pc->data, pc->data_size);
+ switch (eval)
+ {
+ case GNUNET_BLOCK_EVALUATION_OK_MORE:
+ case GNUNET_BLOCK_EVALUATION_OK_LAST:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Good REPLIES matched against routing table"),
+ 1, GNUNET_NO);
+ GDS_NEIGHBOURS_handle_reply (&rr->peer, pc->type, pc->expiration_time, key,
+ ppl, pc->put_path, gpl, pc->get_path,
pc->data,
+ pc->data_size);
+ break;
+ case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Duplicate REPLIES matched against routing
table"),
+ 1, GNUNET_NO);
+ return GNUNET_OK;
+ case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Invalid REPLIES matched against routing
table"),
+ 1, GNUNET_NO);
+ return GNUNET_SYSERR;
+ case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Irrelevant REPLIES matched against routing
table"),
+ 1, GNUNET_NO);
+ return GNUNET_OK;
+ case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
+ GNUNET_break (0);
+ return GNUNET_OK;
+ case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
+ GNUNET_break (0);
+ return GNUNET_OK;
+ case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Unsupported REPLIES matched against routing
table"),
+ 1, GNUNET_NO);
+ return GNUNET_SYSERR;
+ default:
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Handle a reply (route to origin). Only forwards the reply back to
+ * other peers waiting for it. Does not do local caching or
+ * forwarding to local clients. Essentially calls
+ * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
+ * request recently.
+ *
+ * @param type type of the block
+ * @param expiration_time when does the content expire
+ * @param key key for the content
+ * @param put_path_length number of entries in put_path
+ * @param put_path peers the original PUT traversed (if tracked)
+ * @param get_path_length number of entries in get_path
+ * @param get_path peers this reply has traversed so far (if tracked)
+ * @param data payload of the reply
+ * @param data_size number of bytes in data
+ */
+void
+GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
+ struct GNUNET_TIME_Absolute expiration_time,
+ const struct GNUNET_HashCode * key, unsigned int
put_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *get_path,
+ const void *data, size_t data_size)
+{
+ struct ProcessContext pc;
+
+ pc.type = type;
+ pc.expiration_time = expiration_time;
+ pc.put_path_length = put_path_length;
+ pc.put_path = put_path;
+ pc.get_path_length = get_path_length;
+ pc.get_path = get_path;
+ pc.data = data;
+ pc.data_size = data_size;
+ if (NULL == data)
+ {
+ /* Some apps might have an 'empty' reply as a valid reply; however,
+ 'process' will call GNUNET_BLOCK_evaluate' which treats a 'NULL'
+ reply as request-validation (but we need response-validation).
+ So we set 'data' to a 0-byte non-NULL value just to be sure */
+ GNUNET_break (0 == data_size);
+ pc.data_size = 0;
+ pc.data = ""; /* something not null */
+ }
+ GNUNET_CONTAINER_multihashmap_get_multiple (recent_map, key, &process, &pc);
+}
+
+
+/**
+ * Remove the oldest entry from the DHT routing table. Must only
+ * be called if it is known that there is at least one entry
+ * in the heap and hashmap.
+ */
+static void
+expire_oldest_entry ()
+{
+ struct RecentRequest *recent_req;
+
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# Entries removed from routing table"), 1,
+ GNUNET_NO);
+ recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
+ GNUNET_assert (recent_req != NULL);
+ GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
+ GNUNET_CONTAINER_bloomfilter_free (recent_req->reply_bf);
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (recent_map,
+ &recent_req->key,
+ recent_req));
+ GNUNET_free (recent_req);
+}
+
+
+/**
+ * Try to combine multiple recent requests for the same value
+ * (if they come from the same peer).
+ *
+ * @param cls the new 'struct RecentRequest' (to discard upon successful
combination)
+ * @param key the query
+ * @param value the existing 'struct RecentRequest' (to update upon successful
combination)
+ * @return GNUNET_OK (continue to iterate),
+ * GNUNET_SYSERR if the request was successfully combined
+ */
+static int
+try_combine_recent (void *cls, const struct GNUNET_HashCode * key, void *value)
+{
+ struct RecentRequest *in = cls;
+ struct RecentRequest *rr = value;
+
+ if ( (0 != memcmp (&in->peer,
+ &rr->peer,
+ sizeof (struct GNUNET_PeerIdentity))) ||
+ (in->type != rr->type) ||
+ (in->xquery_size != rr->xquery_size) ||
+ (0 != memcmp (in->xquery,
+ rr->xquery,
+ in->xquery_size)) )
+ return GNUNET_OK;
+ if (in->reply_bf_mutator != rr->reply_bf_mutator)
+ {
+ rr->reply_bf_mutator = in->reply_bf_mutator;
+ GNUNET_CONTAINER_bloomfilter_free (rr->reply_bf);
+ rr->reply_bf = in->reply_bf;
+ }
+ else
+ {
+ GNUNET_CONTAINER_bloomfilter_or2 (rr->reply_bf,
+ in->reply_bf);
+ GNUNET_CONTAINER_bloomfilter_free (in->reply_bf);
+ }
+ GNUNET_free (in);
+ return GNUNET_SYSERR;
+}
+
+
+/**
+ * Add a new entry to our routing table.
+ *
+ * @param sender peer that originated the request
+ * @param type type of the block
+ * @param options options for processing
+ * @param key key for the content
+ * @param xquery extended query
+ * @param xquery_size number of bytes in @a xquery
+ * @param reply_bf bloomfilter to filter duplicates
+ * @param reply_bf_mutator mutator for @a reply_bf
+ */
+void
+GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender,
+ enum GNUNET_BLOCK_Type type,
+ enum GNUNET_DHT_RouteOption options,
+ const struct GNUNET_HashCode * key, const void *xquery,
+ size_t xquery_size,
+ const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
+ uint32_t reply_bf_mutator)
+{
+ struct RecentRequest *recent_req;
+
+ while (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT)
+ expire_oldest_entry ();
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Entries added to routing table"),
+ 1, GNUNET_NO);
+ recent_req = GNUNET_malloc (sizeof (struct RecentRequest) + xquery_size);
+ recent_req->peer = *sender;
+ recent_req->key = *key;
+ recent_req->reply_bf = GNUNET_CONTAINER_bloomfilter_copy (reply_bf);
+ recent_req->type = type;
+ recent_req->options = options;
+ recent_req->xquery = &recent_req[1];
+ memcpy (&recent_req[1], xquery, xquery_size);
+ recent_req->xquery_size = xquery_size;
+ recent_req->reply_bf_mutator = reply_bf_mutator;
+ if (GNUNET_SYSERR ==
+ GNUNET_CONTAINER_multihashmap_get_multiple (recent_map, key,
+ &try_combine_recent,
recent_req))
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop
+ ("# DHT requests combined"),
+ 1, GNUNET_NO);
+ return;
+ }
+ recent_req->heap_node =
+ GNUNET_CONTAINER_heap_insert (recent_heap, recent_req,
+ GNUNET_TIME_absolute_get ().abs_value_us);
+ GNUNET_CONTAINER_multihashmap_put (recent_map, key, recent_req,
+
GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+
+
+}
+
+
+/**
+ * Initialize routing subsystem.
+ */
+void
+GDS_ROUTING_init ()
+{
+ recent_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
+ recent_map = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT * 4 / 3,
GNUNET_NO);
+}
+
+
+/**
+ * Shutdown routing subsystem.
+ */
+void
+GDS_ROUTING_done ()
+{
+ while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0)
+ expire_oldest_entry ();
+ GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent_heap));
+ GNUNET_CONTAINER_heap_destroy (recent_heap);
+ recent_heap = NULL;
+ GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent_map));
+ GNUNET_CONTAINER_multihashmap_destroy (recent_map);
+ recent_map = NULL;
+}
+
+/* end of gnunet-service-dht_routing.c */
Copied: gnunet/src/dht/gnunet-service-xdht_routing.h (from rev 31026,
gnunet/src/dht/gnunet-service-dht_routing.h)
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_routing.h
(rev 0)
+++ gnunet/src/dht/gnunet-service-xdht_routing.h 2013-12-05 10:25:18 UTC
(rev 31062)
@@ -0,0 +1,96 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ 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 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.
+
+ 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.
+*/
+
+/**
+ * @file dht/gnunet-service-xdht_routing.h
+ * @brief GNUnet DHT tracking of requests for routing replies
+ * @author Christian Grothoff
+ */
+#ifndef GNUNET_SERVICE_DHT_ROUTING_H
+#define GNUNET_SERVICE_DHT_ROUTING_H
+
+#include "gnunet_util_lib.h"
+#include "gnunet_block_lib.h"
+#include "gnunet_dht_service.h"
+
+
+/**
+ * Handle a reply (route to origin). Only forwards the reply back to
+ * other peers waiting for it. Does not do local caching or
+ * forwarding to local clients. Essentially calls
+ * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
+ * request recently.
+ *
+ * @param type type of the block
+ * @param expiration_time when does the content expire
+ * @param key key for the content
+ * @param put_path_length number of entries in @a put_path
+ * @param put_path peers the original PUT traversed (if tracked)
+ * @param get_path_length number of entries in @a get_path
+ * @param get_path peers this reply has traversed so far (if tracked)
+ * @param data payload of the reply
+ * @param data_size number of bytes in @a data
+ */
+void
+GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
+ struct GNUNET_TIME_Absolute expiration_time,
+ const struct GNUNET_HashCode * key, unsigned int
put_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *get_path,
+ const void *data, size_t data_size);
+
+
+/**
+ * Add a new entry to our routing table.
+ *
+ * @param sender peer that originated the request
+ * @param type type of the block
+ * @param options options for processing
+ * @param key key for the content
+ * @param xquery extended query
+ * @param xquery_size number of bytes in @a xquery
+ * @param reply_bf bloomfilter to filter duplicates
+ * @param reply_bf_mutator mutator for @a reply_bf
+*/
+void
+GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender,
+ enum GNUNET_BLOCK_Type type,
+ enum GNUNET_DHT_RouteOption options,
+ const struct GNUNET_HashCode * key, const void *xquery,
+ size_t xquery_size,
+ const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
+ uint32_t reply_bf_mutator);
+
+
+/**
+ * Initialize routing subsystem.
+ */
+void
+GDS_ROUTING_init (void);
+
+
+/**
+ * Shutdown routing subsystem.
+ */
+void
+GDS_ROUTING_done (void);
+
+#endif
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r31062 - gnunet/src/dht,
gnunet <=