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 * Block group for filtering replies.
63 struct GNUNET_BLOCK_Group *bg;
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.
84 enum GNUNET_DHT_RouteOption options;
90 * Recent requests by time inserted.
92 static struct GNUNET_CONTAINER_Heap *recent_heap;
95 * Recently seen requests by key.
97 static struct GNUNET_CONTAINER_MultiHashMap *recent_map;
101 * Closure for the 'process' function.
103 struct ProcessContext
106 * Path of the original PUT
108 const struct GNUNET_PeerIdentity *put_path;
113 const struct GNUNET_PeerIdentity *get_path;
116 * Payload of the reply.
121 * Expiration time of the result.
123 struct GNUNET_TIME_Absolute expiration_time;
126 * Number of entries in @e put_path.
128 unsigned int put_path_length;
131 * Number of entries in @e get_path.
133 unsigned int get_path_length;
136 * Number of bytes in @e data.
143 enum GNUNET_BLOCK_Type type;
149 * Forward the result to the given peer if it matches the request.
151 * @param cls the `struct ProcessContext` with the result
152 * @param key the query
153 * @param value the `struct RecentRequest` with the request
154 * @return #GNUNET_OK (continue to iterate),
155 * #GNUNET_SYSERR if the result is malformed or type unsupported
159 const struct GNUNET_HashCode *key,
162 struct ProcessContext *pc = cls;
163 struct RecentRequest *rr = value;
164 enum GNUNET_BLOCK_EvaluationResult eval;
167 struct GNUNET_HashCode hc;
168 const struct GNUNET_HashCode *eval_key;
170 if ( (rr->type != GNUNET_BLOCK_TYPE_ANY) &&
171 (rr->type != pc->type) )
172 return GNUNET_OK; /* type missmatch */
174 if (0 != (rr->options & GNUNET_DHT_RO_RECORD_ROUTE))
176 gpl = pc->get_path_length;
177 ppl = pc->put_path_length;
184 if ( (0 != (rr->options & GNUNET_DHT_RO_FIND_PEER)) &&
185 (pc->type == GNUNET_BLOCK_TYPE_DHT_HELLO) )
187 /* key may not match HELLO, which is OK since
188 * the search is approximate. Still, the evaluation
189 * would fail since the match is not exact. So
190 * we fake it by changing the key to the actual PID ... */
191 GNUNET_BLOCK_get_key (GDS_block_context,
192 GNUNET_BLOCK_TYPE_DHT_HELLO,
203 = GNUNET_BLOCK_evaluate (GDS_block_context,
206 GNUNET_BLOCK_EO_NONE,
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,
229 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
230 GNUNET_STATISTICS_update (GDS_stats,
232 ("# Duplicate REPLIES matched against routing table"),
235 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
236 GNUNET_STATISTICS_update (GDS_stats,
238 ("# Invalid REPLIES matched against routing table"),
240 return GNUNET_SYSERR;
241 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
242 GNUNET_STATISTICS_update (GDS_stats,
244 ("# Irrelevant REPLIES matched against routing table"),
247 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
250 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
253 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
254 GNUNET_STATISTICS_update (GDS_stats,
256 ("# Unsupported REPLIES matched against routing table"),
258 return GNUNET_SYSERR;
261 return GNUNET_SYSERR;
268 * Handle a reply (route to origin). Only forwards the reply back to
269 * other peers waiting for it. Does not do local caching or
270 * forwarding to local clients. Essentially calls
271 * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
274 * @param type type of the block
275 * @param expiration_time when does the content expire
276 * @param key key for the content
277 * @param put_path_length number of entries in @a put_path
278 * @param put_path peers the original PUT traversed (if tracked)
279 * @param get_path_length number of entries in @a get_path
280 * @param get_path peers this reply has traversed so far (if tracked)
281 * @param data payload of the reply
282 * @param data_size number of bytes in data
285 GDS_ROUTING_process (void *cls,
286 enum GNUNET_BLOCK_Type type,
287 struct GNUNET_TIME_Absolute expiration_time,
288 const struct GNUNET_HashCode *key,
289 unsigned int put_path_length,
290 const struct GNUNET_PeerIdentity *put_path,
291 unsigned int get_path_length,
292 const struct GNUNET_PeerIdentity *get_path,
296 struct ProcessContext pc;
299 pc.expiration_time = expiration_time;
300 pc.put_path_length = put_path_length;
301 pc.put_path = put_path;
302 pc.get_path_length = get_path_length;
303 pc.get_path = get_path;
305 pc.data_size = data_size;
308 /* Some apps might have an 'empty' reply as a valid reply; however,
309 'process' will call GNUNET_BLOCK_evaluate' which treats a 'NULL'
310 reply as request-validation (but we need response-validation).
311 So we set 'data' to a 0-byte non-NULL value just to be sure */
312 GNUNET_break (0 == data_size);
314 pc.data = ""; /* something not null */
316 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map,
324 * Remove the oldest entry from the DHT routing table. Must only
325 * be called if it is known that there is at least one entry
326 * in the heap and hashmap.
329 expire_oldest_entry ()
331 struct RecentRequest *recent_req;
333 GNUNET_STATISTICS_update (GDS_stats,
335 ("# Entries removed from routing table"), 1,
337 recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
338 GNUNET_assert (recent_req != NULL);
339 GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
340 GNUNET_BLOCK_group_destroy (recent_req->bg);
341 GNUNET_assert (GNUNET_YES ==
342 GNUNET_CONTAINER_multihashmap_remove (recent_map,
345 GNUNET_free (recent_req);
350 * Try to combine multiple recent requests for the same value
351 * (if they come from the same peer).
353 * @param cls the new 'struct RecentRequest' (to discard upon successful combination)
354 * @param key the query
355 * @param value the existing 'struct RecentRequest' (to update upon successful combination)
356 * @return #GNUNET_OK (continue to iterate),
357 * #GNUNET_SYSERR if the request was successfully combined
360 try_combine_recent (void *cls,
361 const struct GNUNET_HashCode *key,
364 struct RecentRequest *in = cls;
365 struct RecentRequest *rr = value;
367 if ( (0 != memcmp (&in->peer,
369 sizeof (struct GNUNET_PeerIdentity))) ||
370 (in->type != rr->type) ||
371 (in->xquery_size != rr->xquery_size) ||
372 (0 != memcmp (in->xquery,
376 GNUNET_break (GNUNET_SYSERR !=
377 GNUNET_BLOCK_group_merge (in->bg,
381 return GNUNET_SYSERR;
386 * Add a new entry to our routing table.
388 * @param sender peer that originated the request
389 * @param type type of the block
390 * @param options options for processing
391 * @param key key for the content
392 * @param xquery extended query
393 * @param xquery_size number of bytes in @a xquery
394 * @param reply_bf bloomfilter to filter duplicates
395 * @param reply_bf_mutator mutator for @a reply_bf
398 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender,
399 enum GNUNET_BLOCK_Type type,
400 struct GNUNET_BLOCK_Group *bg,
401 enum GNUNET_DHT_RouteOption options,
402 const struct GNUNET_HashCode *key,
406 struct RecentRequest *recent_req;
408 while (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT)
409 expire_oldest_entry ();
410 GNUNET_STATISTICS_update (GDS_stats,
411 gettext_noop ("# Entries added to routing table"),
414 recent_req = GNUNET_malloc (sizeof (struct RecentRequest) + xquery_size);
415 recent_req->peer = *sender;
416 recent_req->key = *key;
418 recent_req->type = type;
419 recent_req->options = options;
420 recent_req->xquery = &recent_req[1];
421 GNUNET_memcpy (&recent_req[1],
424 recent_req->xquery_size = xquery_size;
426 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map,
431 GNUNET_STATISTICS_update (GDS_stats,
433 ("# DHT requests combined"),
437 recent_req->heap_node
438 = GNUNET_CONTAINER_heap_insert (recent_heap,
440 GNUNET_TIME_absolute_get ().abs_value_us);
441 GNUNET_CONTAINER_multihashmap_put (recent_map,
444 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
449 * Initialize routing subsystem.
454 recent_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
455 recent_map = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
460 * Shutdown routing subsystem.
465 while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0)
466 expire_oldest_entry ();
467 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent_heap));
468 GNUNET_CONTAINER_heap_destroy (recent_heap);
470 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent_map));
471 GNUNET_CONTAINER_multihashmap_destroy (recent_map);
475 /* end of gnunet-service-dht_routing.c */