2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file dht/gnunet-service-dht_routing.c
23 * @brief GNUnet DHT tracking of requests for routing replies
24 * @author Christian Grothoff
27 #include "gnunet-service-dht_neighbours.h"
28 #include "gnunet-service-dht_routing.h"
29 #include "gnunet-service-dht.h"
33 * Number of requests we track at most (for routing replies).
35 #define DHT_MAX_RECENT (1024 * 16)
39 * Information we keep about all recent GET requests
40 * so that we can route replies.
46 * The peer this request was received from.
48 struct GNUNET_PeerIdentity peer;
51 * Key of this request.
53 struct GNUNET_HashCode key;
56 * Position of this node in the min heap.
58 struct GNUNET_CONTAINER_HeapNode *heap_node;
61 * Bloomfilter for replies to drop.
63 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
66 * Type of the requested block.
68 enum GNUNET_BLOCK_Type type;
71 * extended query (see gnunet_block_lib.h). Allocated at the
77 * Number of bytes in xquery.
82 * Mutator value for the reply_bf, see gnunet_block_lib.h
84 uint32_t reply_bf_mutator;
89 enum GNUNET_DHT_RouteOption options;
95 * Recent requests by time inserted.
97 static struct GNUNET_CONTAINER_Heap *recent_heap;
100 * Recently seen requests by key.
102 static struct GNUNET_CONTAINER_MultiHashMap *recent_map;
106 * Closure for the 'process' function.
108 struct ProcessContext
111 * Path of the original PUT
113 const struct GNUNET_PeerIdentity *put_path;
118 const struct GNUNET_PeerIdentity *get_path;
121 * Payload of the reply.
126 * Expiration time of the result.
128 struct GNUNET_TIME_Absolute expiration_time;
131 * Number of entries in 'put_path'.
133 unsigned int put_path_length;
136 * Number of entries in 'get_path'.
138 unsigned int get_path_length;
141 * Number of bytes in 'data'.
148 enum GNUNET_BLOCK_Type type;
154 * Forward the result to the given peer if it matches the request.
156 * @param cls the `struct ProcessContext` with the result
157 * @param key the query
158 * @param value the `struct RecentRequest` with the request
159 * @return #GNUNET_OK (continue to iterate),
160 * #GNUNET_SYSERR if the result is malformed or type unsupported
163 process (void *cls, const struct GNUNET_HashCode * key, void *value)
165 struct ProcessContext *pc = cls;
166 struct RecentRequest *rr = value;
167 enum GNUNET_BLOCK_EvaluationResult eval;
170 struct GNUNET_HashCode hc;
171 const struct GNUNET_HashCode *eval_key;
173 if ((rr->type != GNUNET_BLOCK_TYPE_ANY) && (rr->type != pc->type))
174 return GNUNET_OK; /* type missmatch */
176 if (0 != (rr->options & GNUNET_DHT_RO_RECORD_ROUTE))
178 gpl = pc->get_path_length;
179 ppl = pc->put_path_length;
186 if ((0 != (rr->options & GNUNET_DHT_RO_FIND_PEER)) &&
187 (pc->type == GNUNET_BLOCK_TYPE_DHT_HELLO))
189 /* key may not match HELLO, which is OK since
190 * the search is approximate. Still, the evaluation
191 * would fail since the match is not exact. So
192 * we fake it by changing the key to the actual PID ... */
193 GNUNET_BLOCK_get_key (GDS_block_context, GNUNET_BLOCK_TYPE_DHT_HELLO,
194 pc->data, pc->data_size, &hc);
202 GNUNET_BLOCK_evaluate (GDS_block_context,
204 GNUNET_BLOCK_EO_NONE,
207 rr->reply_bf_mutator,
214 case GNUNET_BLOCK_EVALUATION_OK_MORE:
215 case GNUNET_BLOCK_EVALUATION_OK_LAST:
216 GNUNET_STATISTICS_update (GDS_stats,
218 ("# Good REPLIES matched against routing table"),
220 GDS_NEIGHBOURS_handle_reply (&rr->peer, pc->type, pc->expiration_time, key,
221 ppl, pc->put_path, gpl, pc->get_path, pc->data,
224 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
225 GNUNET_STATISTICS_update (GDS_stats,
227 ("# Duplicate REPLIES matched against routing table"),
230 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
231 GNUNET_STATISTICS_update (GDS_stats,
233 ("# Invalid REPLIES matched against routing table"),
235 return GNUNET_SYSERR;
236 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
237 GNUNET_STATISTICS_update (GDS_stats,
239 ("# Irrelevant REPLIES matched against routing table"),
242 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
245 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
248 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
249 GNUNET_STATISTICS_update (GDS_stats,
251 ("# Unsupported REPLIES matched against routing table"),
253 return GNUNET_SYSERR;
256 return GNUNET_SYSERR;
263 * Handle a reply (route to origin). Only forwards the reply back to
264 * other peers waiting for it. Does not do local caching or
265 * forwarding to local clients. Essentially calls
266 * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
269 * @param type type of the block
270 * @param expiration_time when does the content expire
271 * @param key key for the content
272 * @param put_path_length number of entries in put_path
273 * @param put_path peers the original PUT traversed (if tracked)
274 * @param get_path_length number of entries in get_path
275 * @param get_path peers this reply has traversed so far (if tracked)
276 * @param data payload of the reply
277 * @param data_size number of bytes in data
280 GDS_ROUTING_process (void *cls,
281 enum GNUNET_BLOCK_Type type,
282 struct GNUNET_TIME_Absolute expiration_time,
283 const struct GNUNET_HashCode *key,
284 unsigned int put_path_length,
285 const struct GNUNET_PeerIdentity *put_path,
286 unsigned int get_path_length,
287 const struct GNUNET_PeerIdentity *get_path,
291 struct ProcessContext pc;
294 pc.expiration_time = expiration_time;
295 pc.put_path_length = put_path_length;
296 pc.put_path = put_path;
297 pc.get_path_length = get_path_length;
298 pc.get_path = get_path;
300 pc.data_size = data_size;
303 /* Some apps might have an 'empty' reply as a valid reply; however,
304 'process' will call GNUNET_BLOCK_evaluate' which treats a 'NULL'
305 reply as request-validation (but we need response-validation).
306 So we set 'data' to a 0-byte non-NULL value just to be sure */
307 GNUNET_break (0 == data_size);
309 pc.data = ""; /* something not null */
311 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map, key, &process, &pc);
316 * Remove the oldest entry from the DHT routing table. Must only
317 * be called if it is known that there is at least one entry
318 * in the heap and hashmap.
321 expire_oldest_entry ()
323 struct RecentRequest *recent_req;
325 GNUNET_STATISTICS_update (GDS_stats,
327 ("# Entries removed from routing table"), 1,
329 recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
330 GNUNET_assert (recent_req != NULL);
331 GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
332 GNUNET_CONTAINER_bloomfilter_free (recent_req->reply_bf);
333 GNUNET_assert (GNUNET_YES ==
334 GNUNET_CONTAINER_multihashmap_remove (recent_map,
337 GNUNET_free (recent_req);
342 * Try to combine multiple recent requests for the same value
343 * (if they come from the same peer).
345 * @param cls the new 'struct RecentRequest' (to discard upon successful combination)
346 * @param key the query
347 * @param value the existing 'struct RecentRequest' (to update upon successful combination)
348 * @return GNUNET_OK (continue to iterate),
349 * GNUNET_SYSERR if the request was successfully combined
352 try_combine_recent (void *cls, const struct GNUNET_HashCode * key, void *value)
354 struct RecentRequest *in = cls;
355 struct RecentRequest *rr = value;
357 if ( (0 != memcmp (&in->peer,
359 sizeof (struct GNUNET_PeerIdentity))) ||
360 (in->type != rr->type) ||
361 (in->xquery_size != rr->xquery_size) ||
362 (0 != memcmp (in->xquery,
366 if (in->reply_bf_mutator != rr->reply_bf_mutator)
368 rr->reply_bf_mutator = in->reply_bf_mutator;
369 GNUNET_CONTAINER_bloomfilter_free (rr->reply_bf);
370 rr->reply_bf = in->reply_bf;
374 GNUNET_CONTAINER_bloomfilter_or2 (rr->reply_bf,
376 GNUNET_CONTAINER_bloomfilter_free (in->reply_bf);
379 return GNUNET_SYSERR;
384 * Add a new entry to our routing table.
386 * @param sender peer that originated the request
387 * @param type type of the block
388 * @param options options for processing
389 * @param key key for the content
390 * @param xquery extended query
391 * @param xquery_size number of bytes in @a xquery
392 * @param reply_bf bloomfilter to filter duplicates
393 * @param reply_bf_mutator mutator for @a reply_bf
396 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender,
397 enum GNUNET_BLOCK_Type type,
398 enum GNUNET_DHT_RouteOption options,
399 const struct GNUNET_HashCode * key, const void *xquery,
401 const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
402 uint32_t reply_bf_mutator)
404 struct RecentRequest *recent_req;
406 while (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT)
407 expire_oldest_entry ();
408 GNUNET_STATISTICS_update (GDS_stats,
409 gettext_noop ("# Entries added to routing table"),
411 recent_req = GNUNET_malloc (sizeof (struct RecentRequest) + xquery_size);
412 recent_req->peer = *sender;
413 recent_req->key = *key;
414 recent_req->reply_bf = GNUNET_CONTAINER_bloomfilter_copy (reply_bf);
415 recent_req->type = type;
416 recent_req->options = options;
417 recent_req->xquery = &recent_req[1];
418 GNUNET_memcpy (&recent_req[1], xquery, xquery_size);
419 recent_req->xquery_size = xquery_size;
420 recent_req->reply_bf_mutator = reply_bf_mutator;
422 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map, key,
423 &try_combine_recent, recent_req))
425 GNUNET_STATISTICS_update (GDS_stats,
427 ("# DHT requests combined"),
431 recent_req->heap_node =
432 GNUNET_CONTAINER_heap_insert (recent_heap, recent_req,
433 GNUNET_TIME_absolute_get ().abs_value_us);
434 GNUNET_CONTAINER_multihashmap_put (recent_map, key, recent_req,
435 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
442 * Initialize routing subsystem.
447 recent_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
448 recent_map = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
453 * Shutdown routing subsystem.
458 while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0)
459 expire_oldest_entry ();
460 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent_heap));
461 GNUNET_CONTAINER_heap_destroy (recent_heap);
463 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent_map));
464 GNUNET_CONTAINER_multihashmap_destroy (recent_map);
468 /* end of gnunet-service-dht_routing.c */