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_routing.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 * Position of this node in the min heap.
51 struct GNUNET_CONTAINER_HeapNode *heap_node;
54 * Bloomfilter for replies to drop.
56 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
59 * Timestamp of this request, for ordering
62 struct GNUNET_TIME_Absolute timestamp;
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.
81 * Mutator value for the reply_bf, see gnunet_block_lib.h
83 uint32_t reply_bf_mutator;
86 * Key of this request.
94 * Recent requests by time inserted.
96 static struct GNUNET_CONTAINER_Heap *recent_heap;
99 * Recently seen requests by key.
101 static struct GNUNET_CONTAINER_MultiHashMap *recent_map;
105 * Handle a reply (route to origin). Only forwards the reply back to
106 * other peers waiting for it. Does not do local caching or
107 * forwarding to local clients. Essentially calls
108 * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
111 * @param type type of the block
112 * @param expiration_time when does the content expire
113 * @param key key for the content
114 * @param put_path_length number of entries in put_path
115 * @param put_path peers the original PUT traversed (if tracked)
116 * @param get_path_length number of entries in put_path
117 * @param get_path peers this reply has traversed so far (if tracked)
118 * @param data payload of the reply
119 * @param data_size number of bytes in data
122 GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
123 GNUNET_TIME_Absolute expiration_time,
124 const GNUNET_HashCode *key,
125 unsigned int put_path_length,
126 struct GNUNET_PeerIdentity *put_path,
127 unsigned int get_path_length,
128 struct GNUNET_PeerIdentity *get_path,
136 * Add a new entry to our routing table.
138 * @param sender peer that originated the request
139 * @param type type of the block
140 * @param key key for the content
141 * @param xquery extended query
142 * @param xquery_size number of bytes in xquery
143 * @param reply_bf bloomfilter to filter duplicates
144 * @param reply_bf_mutator mutator for reply_bf
147 GDS_ROUTING_add (const GNUNET_PeerIdentity *sender,
148 enum GNUNET_BLOCK_Type type,
149 const GNUNET_HashCode *key,
152 const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
153 uint32_t reply_bf_mutator)
155 if (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT)
157 recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
158 GNUNET_assert (recent_req != NULL);
159 GNUNET_SCHEDULER_cancel (recent_req->remove_task);
160 GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
161 GNUNET_CONTAINER_bloomfilter_free (recent_req->bloom);
162 GNUNET_free (recent_req);
165 recent_req = GNUNET_malloc (sizeof (struct RecentRequest));
166 recent_req->uid = msg_ctx->unique_id;
167 memcpy (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
168 recent_req->heap_node =
169 GNUNET_CONTAINER_heap_insert (recent_heap, recent_req,
170 GNUNET_TIME_absolute_get ().abs_value);
172 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
179 * Initialize routing subsystem.
185 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
187 GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
192 * Shutdown routing subsystem.
197 while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0)
199 recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
200 GNUNET_assert (recent_req != NULL);
201 GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
202 GNUNET_CONTAINER_bloomfilter_free (recent_req->bloom);
203 GNUNET_free (recent_req);
205 GNUNET_CONTAINER_heap_destroy (recent_heap);
207 GNUNET_CONTAINER_multihashmap_destroy (recent_map);
211 /* end of gnunet-service-dht_routing.c */