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/>.
20 * @file dht/gnunet-service-dht_routing.c
21 * @brief GNUnet DHT tracking of requests for routing replies
22 * @author Christian Grothoff
25 #include "gnunet-service-dht_neighbours.h"
26 #include "gnunet-service-dht_routing.h"
27 #include "gnunet-service-dht.h"
31 * Number of requests we track at most (for routing replies).
33 #define DHT_MAX_RECENT (1024 * 16)
37 * Information we keep about all recent GET requests
38 * so that we can route replies.
44 * The peer this request was received from.
46 struct GNUNET_PeerIdentity peer;
49 * Key of this request.
51 struct GNUNET_HashCode key;
54 * Position of this node in the min heap.
56 struct GNUNET_CONTAINER_HeapNode *heap_node;
59 * Block group for filtering replies.
61 struct GNUNET_BLOCK_Group *bg;
64 * Type of the requested block.
66 enum GNUNET_BLOCK_Type type;
69 * extended query (see gnunet_block_lib.h). Allocated at the
75 * Number of bytes in xquery.
82 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;
147 * Forward the result to the given peer if it matches the request.
149 * @param cls the `struct ProcessContext` with the result
150 * @param key the query
151 * @param value the `struct RecentRequest` with the request
152 * @return #GNUNET_OK (continue to iterate),
153 * #GNUNET_SYSERR if the result is malformed or type unsupported
157 const struct GNUNET_HashCode *key,
160 struct ProcessContext *pc = cls;
161 struct RecentRequest *rr = value;
162 enum GNUNET_BLOCK_EvaluationResult eval;
165 struct GNUNET_HashCode hc;
166 const struct GNUNET_HashCode *eval_key;
168 if ( (rr->type != GNUNET_BLOCK_TYPE_ANY) &&
169 (rr->type != pc->type) )
170 return GNUNET_OK; /* type missmatch */
172 if (0 != (rr->options & GNUNET_DHT_RO_RECORD_ROUTE))
174 gpl = pc->get_path_length;
175 ppl = pc->put_path_length;
182 if ( (0 != (rr->options & GNUNET_DHT_RO_FIND_PEER)) &&
183 (pc->type == GNUNET_BLOCK_TYPE_DHT_HELLO) )
185 /* key may not match HELLO, which is OK since
186 * the search is approximate. Still, the evaluation
187 * would fail since the match is not exact. So
188 * we fake it by changing the key to the actual PID ... */
189 GNUNET_BLOCK_get_key (GDS_block_context,
190 GNUNET_BLOCK_TYPE_DHT_HELLO,
201 = GNUNET_BLOCK_evaluate (GDS_block_context,
204 GNUNET_BLOCK_EO_NONE,
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "Result for %s of type %d was evaluated as %d\n",
217 case GNUNET_BLOCK_EVALUATION_OK_MORE:
218 case GNUNET_BLOCK_EVALUATION_OK_LAST:
219 GNUNET_STATISTICS_update (GDS_stats,
221 ("# Good REPLIES matched against routing table"),
223 GDS_NEIGHBOURS_handle_reply (&rr->peer,
232 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
233 GNUNET_STATISTICS_update (GDS_stats,
235 ("# Duplicate REPLIES matched against routing table"),
238 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
239 GNUNET_STATISTICS_update (GDS_stats,
241 ("# Invalid REPLIES matched against routing table"),
243 return GNUNET_SYSERR;
244 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
245 GNUNET_STATISTICS_update (GDS_stats,
247 ("# Irrelevant REPLIES matched against routing table"),
250 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
253 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
256 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
257 GNUNET_STATISTICS_update (GDS_stats,
259 ("# Unsupported REPLIES matched against routing table"),
261 return GNUNET_SYSERR;
264 return GNUNET_SYSERR;
271 * Handle a reply (route to origin). Only forwards the reply back to
272 * other peers waiting for it. Does not do local caching or
273 * forwarding to local clients. Essentially calls
274 * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
277 * @param type type of the block
278 * @param expiration_time when does the content expire
279 * @param key key for the content
280 * @param put_path_length number of entries in @a put_path
281 * @param put_path peers the original PUT traversed (if tracked)
282 * @param get_path_length number of entries in @a get_path
283 * @param get_path peers this reply has traversed so far (if tracked)
284 * @param data payload of the reply
285 * @param data_size number of bytes in data
288 GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
289 struct GNUNET_TIME_Absolute expiration_time,
290 const struct GNUNET_HashCode *key,
291 unsigned int put_path_length,
292 const struct GNUNET_PeerIdentity *put_path,
293 unsigned int get_path_length,
294 const struct GNUNET_PeerIdentity *get_path,
298 struct ProcessContext pc;
301 pc.expiration_time = expiration_time;
302 pc.put_path_length = put_path_length;
303 pc.put_path = put_path;
304 pc.get_path_length = get_path_length;
305 pc.get_path = get_path;
307 pc.data_size = data_size;
310 /* Some apps might have an 'empty' reply as a valid reply; however,
311 'process' will call GNUNET_BLOCK_evaluate' which treats a 'NULL'
312 reply as request-validation (but we need response-validation).
313 So we set 'data' to a 0-byte non-NULL value just to be sure */
314 GNUNET_break (0 == data_size);
316 pc.data = ""; /* something not null */
318 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map,
326 * Remove the oldest entry from the DHT routing table. Must only
327 * be called if it is known that there is at least one entry
328 * in the heap and hashmap.
331 expire_oldest_entry ()
333 struct RecentRequest *recent_req;
335 GNUNET_STATISTICS_update (GDS_stats,
337 ("# Entries removed from routing table"), 1,
339 recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
340 GNUNET_assert (recent_req != NULL);
341 GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
342 GNUNET_BLOCK_group_destroy (recent_req->bg);
343 GNUNET_assert (GNUNET_YES ==
344 GNUNET_CONTAINER_multihashmap_remove (recent_map,
347 GNUNET_free (recent_req);
352 * Try to combine multiple recent requests for the same value
353 * (if they come from the same peer).
355 * @param cls the new 'struct RecentRequest' (to discard upon successful combination)
356 * @param key the query
357 * @param value the existing 'struct RecentRequest' (to update upon successful combination)
358 * @return #GNUNET_OK (continue to iterate),
359 * #GNUNET_SYSERR if the request was successfully combined
362 try_combine_recent (void *cls,
363 const struct GNUNET_HashCode *key,
366 struct RecentRequest *in = cls;
367 struct RecentRequest *rr = value;
369 if ( (0 != memcmp (&in->peer,
371 sizeof (struct GNUNET_PeerIdentity))) ||
372 (in->type != rr->type) ||
373 (in->xquery_size != rr->xquery_size) ||
374 (0 != memcmp (in->xquery,
378 GNUNET_break (GNUNET_SYSERR !=
379 GNUNET_BLOCK_group_merge (in->bg,
383 return GNUNET_SYSERR;
388 * Add a new entry to our routing table.
390 * @param sender peer that originated the request
391 * @param type type of the block
392 * @param options options for processing
393 * @param key key for the content
394 * @param xquery extended query
395 * @param xquery_size number of bytes in @a xquery
396 * @param reply_bf bloomfilter to filter duplicates
397 * @param reply_bf_mutator mutator for @a reply_bf
400 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender,
401 enum GNUNET_BLOCK_Type type,
402 struct GNUNET_BLOCK_Group *bg,
403 enum GNUNET_DHT_RouteOption options,
404 const struct GNUNET_HashCode *key,
408 struct RecentRequest *recent_req;
410 while (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT)
411 expire_oldest_entry ();
412 GNUNET_STATISTICS_update (GDS_stats,
413 gettext_noop ("# Entries added to routing table"),
416 recent_req = GNUNET_malloc (sizeof (struct RecentRequest) + xquery_size);
417 recent_req->peer = *sender;
418 recent_req->key = *key;
420 recent_req->type = type;
421 recent_req->options = options;
422 recent_req->xquery = &recent_req[1];
423 GNUNET_memcpy (&recent_req[1],
426 recent_req->xquery_size = xquery_size;
428 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map,
433 GNUNET_STATISTICS_update (GDS_stats,
435 ("# DHT requests combined"),
439 recent_req->heap_node
440 = GNUNET_CONTAINER_heap_insert (recent_heap,
442 GNUNET_TIME_absolute_get ().abs_value_us);
443 GNUNET_CONTAINER_multihashmap_put (recent_map,
446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
451 * Initialize routing subsystem.
456 recent_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
457 recent_map = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
462 * Shutdown routing subsystem.
467 while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0)
468 expire_oldest_entry ();
469 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent_heap));
470 GNUNET_CONTAINER_heap_destroy (recent_heap);
472 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent_map));
473 GNUNET_CONTAINER_multihashmap_destroy (recent_map);
477 /* end of gnunet-service-dht_routing.c */