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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
45 * The peer this request was received from.
47 struct GNUNET_PeerIdentity peer;
50 * Key of this request.
52 struct GNUNET_HashCode key;
55 * Position of this node in the min heap.
57 struct GNUNET_CONTAINER_HeapNode *heap_node;
60 * Block group for filtering replies.
62 struct GNUNET_BLOCK_Group *bg;
65 * Type of the requested block.
67 enum GNUNET_BLOCK_Type type;
70 * extended query (see gnunet_block_lib.h). Allocated at the
76 * Number of bytes in xquery.
83 enum GNUNET_DHT_RouteOption options;
88 * Recent requests by time inserted.
90 static struct GNUNET_CONTAINER_Heap *recent_heap;
93 * Recently seen requests by key.
95 static struct GNUNET_CONTAINER_MultiHashMap *recent_map;
99 * Closure for the 'process' function.
101 struct ProcessContext
104 * Path of the original PUT
106 const struct GNUNET_PeerIdentity *put_path;
111 const struct GNUNET_PeerIdentity *get_path;
114 * Payload of the reply.
119 * Expiration time of the result.
121 struct GNUNET_TIME_Absolute expiration_time;
124 * Number of entries in @e put_path.
126 unsigned int put_path_length;
129 * Number of entries in @e get_path.
131 unsigned int get_path_length;
134 * Number of bytes in @e data.
141 enum GNUNET_BLOCK_Type type;
146 * Forward the result to the given peer if it matches the request.
148 * @param cls the `struct ProcessContext` with the result
149 * @param key the query
150 * @param value the `struct RecentRequest` with the request
151 * @return #GNUNET_OK (continue to iterate),
152 * #GNUNET_SYSERR if the result is malformed or type unsupported
156 const struct GNUNET_HashCode *key,
159 struct ProcessContext *pc = cls;
160 struct RecentRequest *rr = value;
161 enum GNUNET_BLOCK_EvaluationResult eval;
164 struct GNUNET_HashCode hc;
165 const struct GNUNET_HashCode *eval_key;
167 if ((rr->type != GNUNET_BLOCK_TYPE_ANY) &&
168 (rr->type != pc->type))
169 return GNUNET_OK; /* type missmatch */
171 if (0 != (rr->options & GNUNET_DHT_RO_RECORD_ROUTE))
173 gpl = pc->get_path_length;
174 ppl = pc->put_path_length;
181 if ((0 != (rr->options & GNUNET_DHT_RO_FIND_PEER)) &&
182 (pc->type == GNUNET_BLOCK_TYPE_DHT_HELLO))
184 /* key may not match HELLO, which is OK since
185 * the search is approximate. Still, the evaluation
186 * would fail since the match is not exact. So
187 * we fake it by changing the key to the actual PID ... */
188 GNUNET_BLOCK_get_key (GDS_block_context,
189 GNUNET_BLOCK_TYPE_DHT_HELLO,
200 = GNUNET_BLOCK_evaluate (GDS_block_context,
203 GNUNET_BLOCK_EO_NONE,
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Result for %s of type %d was evaluated as %d\n",
216 case GNUNET_BLOCK_EVALUATION_OK_MORE:
217 case GNUNET_BLOCK_EVALUATION_OK_LAST:
218 GNUNET_STATISTICS_update (GDS_stats,
220 ("# Good REPLIES matched against routing table"),
222 GDS_NEIGHBOURS_handle_reply (&rr->peer,
232 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
233 GNUNET_STATISTICS_update (GDS_stats,
236 "# Duplicate REPLIES matched against routing table"),
240 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
241 GNUNET_STATISTICS_update (GDS_stats,
244 "# Invalid REPLIES matched against routing table"),
246 return GNUNET_SYSERR;
248 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
249 GNUNET_STATISTICS_update (GDS_stats,
252 "# Irrelevant REPLIES matched against routing table"),
256 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
260 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
264 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
265 GNUNET_STATISTICS_update (GDS_stats,
268 "# Unsupported REPLIES matched against routing table"),
270 return GNUNET_SYSERR;
274 return GNUNET_SYSERR;
281 * Handle a reply (route to origin). Only forwards the reply back to
282 * other peers waiting for it. Does not do local caching or
283 * forwarding to local clients. Essentially calls
284 * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
287 * @param type type of the block
288 * @param expiration_time when does the content expire
289 * @param key key for the content
290 * @param put_path_length number of entries in @a put_path
291 * @param put_path peers the original PUT traversed (if tracked)
292 * @param get_path_length number of entries in @a get_path
293 * @param get_path peers this reply has traversed so far (if tracked)
294 * @param data payload of the reply
295 * @param data_size number of bytes in data
298 GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
299 struct GNUNET_TIME_Absolute expiration_time,
300 const struct GNUNET_HashCode *key,
301 unsigned int put_path_length,
302 const struct GNUNET_PeerIdentity *put_path,
303 unsigned int get_path_length,
304 const struct GNUNET_PeerIdentity *get_path,
308 struct ProcessContext pc;
311 pc.expiration_time = expiration_time;
312 pc.put_path_length = put_path_length;
313 pc.put_path = put_path;
314 pc.get_path_length = get_path_length;
315 pc.get_path = get_path;
317 pc.data_size = data_size;
320 /* Some apps might have an 'empty' reply as a valid reply; however,
321 'process' will call GNUNET_BLOCK_evaluate' which treats a 'NULL'
322 reply as request-validation (but we need response-validation).
323 So we set 'data' to a 0-byte non-NULL value just to be sure */
324 GNUNET_break (0 == data_size);
326 pc.data = ""; /* something not null */
328 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map,
336 * Remove the oldest entry from the DHT routing table. Must only
337 * be called if it is known that there is at least one entry
338 * in the heap and hashmap.
341 expire_oldest_entry ()
343 struct RecentRequest *recent_req;
345 GNUNET_STATISTICS_update (GDS_stats,
347 ("# Entries removed from routing table"), 1,
349 recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
350 GNUNET_assert (recent_req != NULL);
351 GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
352 GNUNET_BLOCK_group_destroy (recent_req->bg);
353 GNUNET_assert (GNUNET_YES ==
354 GNUNET_CONTAINER_multihashmap_remove (recent_map,
357 GNUNET_free (recent_req);
362 * Try to combine multiple recent requests for the same value
363 * (if they come from the same peer).
365 * @param cls the new 'struct RecentRequest' (to discard upon successful combination)
366 * @param key the query
367 * @param value the existing 'struct RecentRequest' (to update upon successful combination)
368 * @return #GNUNET_OK (continue to iterate),
369 * #GNUNET_SYSERR if the request was successfully combined
372 try_combine_recent (void *cls,
373 const struct GNUNET_HashCode *key,
376 struct RecentRequest *in = cls;
377 struct RecentRequest *rr = value;
379 if ((0 != GNUNET_memcmp (&in->peer,
381 (in->type != rr->type) ||
382 (in->xquery_size != rr->xquery_size) ||
383 (0 != memcmp (in->xquery,
387 GNUNET_break (GNUNET_SYSERR !=
388 GNUNET_BLOCK_group_merge (in->bg,
392 return GNUNET_SYSERR;
397 * Add a new entry to our routing table.
399 * @param sender peer that originated the request
400 * @param type type of the block
401 * @param options options for processing
402 * @param key key for the content
403 * @param xquery extended query
404 * @param xquery_size number of bytes in @a xquery
405 * @param reply_bf bloomfilter to filter duplicates
406 * @param reply_bf_mutator mutator for @a reply_bf
409 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender,
410 enum GNUNET_BLOCK_Type type,
411 struct GNUNET_BLOCK_Group *bg,
412 enum GNUNET_DHT_RouteOption options,
413 const struct GNUNET_HashCode *key,
417 struct RecentRequest *recent_req;
419 while (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT)
420 expire_oldest_entry ();
421 GNUNET_STATISTICS_update (GDS_stats,
422 gettext_noop ("# Entries added to routing table"),
425 recent_req = GNUNET_malloc (sizeof(struct RecentRequest) + xquery_size);
426 recent_req->peer = *sender;
427 recent_req->key = *key;
429 recent_req->type = type;
430 recent_req->options = options;
431 recent_req->xquery = &recent_req[1];
432 GNUNET_memcpy (&recent_req[1],
435 recent_req->xquery_size = xquery_size;
437 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map,
442 GNUNET_STATISTICS_update (GDS_stats,
444 ("# DHT requests combined"),
448 recent_req->heap_node
449 = GNUNET_CONTAINER_heap_insert (recent_heap,
451 GNUNET_TIME_absolute_get ().abs_value_us);
452 GNUNET_CONTAINER_multihashmap_put (recent_map,
455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
460 * Initialize routing subsystem.
465 recent_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
466 recent_map = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT * 4 / 3,
472 * Shutdown routing subsystem.
477 while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0)
478 expire_oldest_entry ();
479 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent_heap));
480 GNUNET_CONTAINER_heap_destroy (recent_heap);
482 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent_map));
483 GNUNET_CONTAINER_multihashmap_destroy (recent_map);
488 /* end of gnunet-service-dht_routing.c */