2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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.
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
164 const GNUNET_HashCode *key,
167 struct ProcessContext *pc = cls;
168 struct RecentRequest *rr = value;
169 enum GNUNET_BLOCK_EvaluationResult eval;
173 if ( (rr->type != GNUNET_BLOCK_TYPE_ANY) &&
174 (rr->type != pc->type) )
175 return GNUNET_OK; /* type missmatch */
177 if (0 != (rr->options & GNUNET_DHT_RO_RECORD_ROUTE))
179 gpl = pc->get_path_length;
180 ppl = pc->put_path_length;
187 eval = GNUNET_BLOCK_evaluate (GDS_block_context,
191 rr->reply_bf_mutator,
198 case GNUNET_BLOCK_EVALUATION_OK_MORE:
199 case GNUNET_BLOCK_EVALUATION_OK_LAST:
200 GDS_NEIGHBOURS_handle_reply (&rr->peer,
211 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
213 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
215 return GNUNET_SYSERR;
216 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
217 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
220 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
221 return GNUNET_SYSERR;
224 return GNUNET_SYSERR;
231 * Handle a reply (route to origin). Only forwards the reply back to
232 * other peers waiting for it. Does not do local caching or
233 * forwarding to local clients. Essentially calls
234 * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
237 * @param type type of the block
238 * @param expiration_time when does the content expire
239 * @param key key for the content
240 * @param put_path_length number of entries in put_path
241 * @param put_path peers the original PUT traversed (if tracked)
242 * @param get_path_length number of entries in put_path
243 * @param get_path peers this reply has traversed so far (if tracked)
244 * @param data payload of the reply
245 * @param data_size number of bytes in data
248 GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
249 struct GNUNET_TIME_Absolute expiration_time,
250 const GNUNET_HashCode *key,
251 unsigned int put_path_length,
252 const struct GNUNET_PeerIdentity *put_path,
253 unsigned int get_path_length,
254 const struct GNUNET_PeerIdentity *get_path,
258 struct ProcessContext pc;
261 pc.expiration_time = expiration_time;
262 pc.put_path_length = put_path_length;
263 pc.put_path = put_path;
264 pc.get_path_length = get_path_length;
265 pc.get_path = get_path;
267 pc.data_size = data_size;
268 GNUNET_CONTAINER_multihashmap_get_multiple (recent_map,
276 * Add a new entry to our routing table.
278 * @param sender peer that originated the request
279 * @param type type of the block
280 * @param options options for processing
281 * @param key key for the content
282 * @param xquery extended query
283 * @param xquery_size number of bytes in xquery
284 * @param reply_bf bloomfilter to filter duplicates
285 * @param reply_bf_mutator mutator for reply_bf
288 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender,
289 enum GNUNET_BLOCK_Type type,
290 enum GNUNET_DHT_RouteOption options,
291 const GNUNET_HashCode *key,
294 const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
295 uint32_t reply_bf_mutator)
297 struct RecentRequest *recent_req;
299 while (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT)
301 recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
302 GNUNET_assert (recent_req != NULL);
303 GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
304 GNUNET_CONTAINER_bloomfilter_free (recent_req->reply_bf);
305 GNUNET_free (recent_req);
308 recent_req = GNUNET_malloc (sizeof (struct RecentRequest) + xquery_size);
309 recent_req->peer = *sender;
310 recent_req->key = *key;
311 recent_req->heap_node =
312 GNUNET_CONTAINER_heap_insert (recent_heap, recent_req,
313 GNUNET_TIME_absolute_get ().abs_value);
314 recent_req->reply_bf =
315 GNUNET_CONTAINER_bloomfilter_copy (reply_bf);
316 recent_req->type = type;
317 recent_req->options = options;
318 recent_req->xquery = &recent_req[1];
319 recent_req->xquery_size = xquery_size;
320 recent_req->reply_bf_mutator = reply_bf_mutator;
321 GNUNET_CONTAINER_multihashmap_put (recent_map,
324 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
331 * Initialize routing subsystem.
337 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
339 GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT * 4 / 3);
344 * Shutdown routing subsystem.
349 struct RecentRequest *recent_req;
351 while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0)
353 recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
354 GNUNET_assert (recent_req != NULL);
355 GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
356 GNUNET_CONTAINER_bloomfilter_free (recent_req->reply_bf);
357 GNUNET_free (recent_req);
359 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent_heap));
360 GNUNET_CONTAINER_heap_destroy (recent_heap);
362 GNUNET_CONTAINER_multihashmap_destroy (recent_map);
366 /* end of gnunet-service-dht_routing.c */