GNUnet  0.11.x
gnunet-service-dht_routing.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011, 2022 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
29 #include "gnunet-service-dht.h"
30 
31 
36 #define DHT_MAX_RECENT (1024 * 128)
37 
38 
44 {
49 
53  struct GNUNET_HashCode key;
54 
59 
64 
69  const void *xquery;
70 
74  size_t xquery_size;
75 
80 
85 };
86 
87 
92 
97 
98 
103 {
108 
113 
117  unsigned int get_path_length;
118 
119 };
120 
121 
130 static enum GNUNET_GenericReturnValue
131 process (void *cls,
132  const struct GNUNET_HashCode *query_hash,
133  void *value)
134 {
135  struct ProcessContext *pc = cls;
136  struct RecentRequest *rr = value;
138  unsigned int get_path_length;
139  struct GDS_DATACACHE_BlockData bdx = *pc->bd;
140 
141  if ( (rr->type != GNUNET_BLOCK_TYPE_ANY) &&
142  (rr->type != pc->bd->type) )
143  return GNUNET_OK; /* type mismatch */
144  if (0 != (rr->options & GNUNET_DHT_RO_RECORD_ROUTE))
145  {
146  get_path_length = pc->get_path_length;
147  }
148  else
149  {
150  get_path_length = 0;
151  bdx.put_path_length = 0;
152  bdx.put_path = NULL;
153  }
154  if ( (0 == (rr->options & GNUNET_DHT_RO_FIND_PEER)) &&
155  (0 != GNUNET_memcmp (query_hash,
156  &bdx.key)) )
157  {
159  "# Inexact matches discarded in exact search",
160  1,
161  GNUNET_NO);
162  return GNUNET_OK; /* exact search, but inexact match */
163  }
165  bdx.type,
166  rr->bg,
167  &bdx.key,
168  rr->xquery,
169  rr->xquery_size,
170  bdx.data,
171  bdx.data_size);
173  "Result for %s of type %d was evaluated as %d\n",
174  GNUNET_h2s (&bdx.key),
175  bdx.type,
176  eval);
177  switch (eval)
178  {
182  {
183  struct PeerInfo *pi;
184 
186  "# Good REPLIES matched against routing table",
187  1,
188  GNUNET_NO);
190  if (NULL == pi)
191  {
192  /* peer disconnected in the meantime, drop reply */
194  "No matching peer for reply for key %s\n",
195  GNUNET_h2s (query_hash));
196  return GNUNET_OK;
197  }
199  &bdx,
200  query_hash,
201  get_path_length, pc->get_path);
202  }
203  break;
206  "# Duplicate REPLIES matched against routing table",
207  1,
208  GNUNET_NO);
209  return GNUNET_OK;
211  GNUNET_break (0);
213  "# Invalid REPLIES matched against routing table",
214  1,
215  GNUNET_NO);
216  return GNUNET_OK;
219  "# Irrelevant REPLIES matched against routing table",
220  1,
221  GNUNET_NO);
222  return GNUNET_OK;
223  default:
224  GNUNET_break (0);
225  return GNUNET_OK;
226  }
227  return GNUNET_OK;
228 }
229 
230 
243 void
245  const struct GNUNET_HashCode *query_hash,
246  unsigned int get_path_length,
247  const struct GNUNET_DHT_PathElement *get_path)
248 {
249  struct ProcessContext pc = {
250  .bd = bd,
251  .get_path = get_path,
252  .get_path_length = get_path_length
253  };
254 
256  query_hash,
257  &process,
258  &pc);
259 }
260 
261 
267 static void
269 {
270  struct RecentRequest *recent_req;
271 
273  "# Old entries removed from routing table",
274  1,
275  GNUNET_NO);
277  GNUNET_assert (recent_req != NULL);
279  GNUNET_BLOCK_group_destroy (recent_req->bg);
282  &recent_req->key,
283  recent_req));
284  GNUNET_free (recent_req);
285 }
286 
287 
298 static enum GNUNET_GenericReturnValue
299 try_combine_recent (void *cls,
300  const struct GNUNET_HashCode *key,
301  void *value)
302 {
303  struct RecentRequest *in = cls;
304  struct RecentRequest *rr = value;
305 
306  if ( (0 != GNUNET_memcmp (&in->peer,
307  &rr->peer)) ||
308  (in->type != rr->type) ||
309  (in->xquery_size != rr->xquery_size) ||
310  (0 != memcmp (in->xquery,
311  rr->xquery,
312  in->xquery_size) ) )
313  return GNUNET_OK;
316  rr->bg));
317  rr->bg = in->bg;
318  GNUNET_free (in);
319  return GNUNET_SYSERR;
320 }
321 
322 
335 void
336 GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender,
337  enum GNUNET_BLOCK_Type type,
338  struct GNUNET_BLOCK_Group *bg,
340  const struct GNUNET_HashCode *key,
341  const void *xquery,
342  size_t xquery_size)
343 {
344  struct RecentRequest *recent_req;
345 
349  "# Entries added to routing table",
350  1,
351  GNUNET_NO);
352  recent_req = GNUNET_malloc (sizeof(struct RecentRequest) + xquery_size);
353  recent_req->peer = *sender;
354  recent_req->key = *key;
355  recent_req->bg = bg;
356  recent_req->type = type;
357  recent_req->options = options;
358  recent_req->xquery = &recent_req[1];
359  GNUNET_memcpy (&recent_req[1],
360  xquery,
361  xquery_size);
362  recent_req->xquery_size = xquery_size;
363  if (GNUNET_SYSERR ==
365  key,
367  recent_req))
368  {
370  "# DHT requests combined",
371  1,
372  GNUNET_NO);
373  return;
374  }
375  recent_req->heap_node
377  recent_heap,
378  recent_req,
379  GNUNET_TIME_absolute_get ().abs_value_us);
381  recent_map,
382  key,
383  recent_req,
385 }
386 
387 
391 void
393 {
396  GNUNET_NO);
397 }
398 
399 
403 void
405 {
408  GNUNET_assert (0 ==
411  recent_heap = NULL;
412  GNUNET_assert (0 ==
415  recent_map = NULL;
416 }
417 
418 
419 /* end of gnunet-service-dht_routing.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static struct GNUNET_PEERINFO_Handle * pi
Handle to peerinfo service.
struct GNUNET_HashCode key
The key used in the DHT.
static char * value
Value of the record to add/remove.
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
GNUnet DHT globals.
struct GNUNET_STATISTICS_Handle * GDS_stats
Handle for the statistics service.
struct GNUNET_BLOCK_Context * GDS_block_context
Our handle to the BLOCK library.
struct PeerInfo * GDS_NEIGHBOURS_lookup_peer(const struct GNUNET_PeerIdentity *target)
Lookup peer by peer's identity.
void GDS_NEIGHBOURS_handle_reply(struct PeerInfo *pi, const struct GDS_DATACACHE_BlockData *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path)
Handle a reply (route to origin).
GNUnet DHT routing code.
static enum GNUNET_GenericReturnValue try_combine_recent(void *cls, const struct GNUNET_HashCode *key, void *value)
Try to combine multiple recent requests for the same value (if they come from the same peer).
void GDS_ROUTING_process(const struct GDS_DATACACHE_BlockData *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path)
Handle a reply (route to origin).
static enum GNUNET_GenericReturnValue process(void *cls, const struct GNUNET_HashCode *query_hash, void *value)
Forward the result to the given peer if it matches the request.
void GDS_ROUTING_done()
Shutdown routing subsystem.
void GDS_ROUTING_add(const struct GNUNET_PeerIdentity *sender, enum GNUNET_BLOCK_Type type, struct GNUNET_BLOCK_Group *bg, enum GNUNET_DHT_RouteOption options, const struct GNUNET_HashCode *key, const void *xquery, size_t xquery_size)
Add a new entry to our routing table.
#define DHT_MAX_RECENT
Number of requests we track at most (for routing replies).
static void expire_oldest_entry(void)
Remove the oldest entry from the DHT routing table.
static struct GNUNET_CONTAINER_MultiHashMap * recent_map
Recently seen requests by key.
static struct GNUNET_CONTAINER_Heap * recent_heap
Recent requests by time inserted.
void GDS_ROUTING_init()
Initialize routing subsystem.
GNUnet DHT tracking of requests for routing replies.
#define GNUNET_log(kind,...)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:92
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
enum GNUNET_BLOCK_ReplyEvaluationResult GNUNET_BLOCK_check_reply(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, struct GNUNET_BLOCK_Group *group, const struct GNUNET_HashCode *query, const void *xquery, size_t xquery_size, const void *reply_block, size_t reply_block_size)
Function called to validate a reply.
Definition: block.c:348
GNUNET_BLOCK_ReplyEvaluationResult
Possible ways for how a block may relate to a query.
enum GNUNET_GenericReturnValue GNUNET_BLOCK_group_merge(struct GNUNET_BLOCK_Group *bg1, struct GNUNET_BLOCK_Group *bg2)
Try merging two block groups.
Definition: block.c:182
void GNUNET_BLOCK_group_destroy(struct GNUNET_BLOCK_Group *bg)
Destroy resources used by a block group.
Definition: block.c:173
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
@ GNUNET_BLOCK_REPLY_OK_MORE
Valid result, and there may be more.
@ GNUNET_BLOCK_REPLY_OK_DUPLICATE
Valid result, but suppressed because it is a duplicate.
@ GNUNET_BLOCK_REPLY_OK_LAST
Last possible valid result.
@ GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED
Specified block type not supported by any plugin.
@ GNUNET_BLOCK_REPLY_INVALID
Block does not match query (invalid result)
@ GNUNET_BLOCK_REPLY_IRRELEVANT
Block does not match xquery (valid result, not relevant for the request)
@ GNUNET_BLOCK_TYPE_ANY
Any type of block, used as a wildcard when searching.
GNUNET_DHT_RouteOption
Options for routing.
@ GNUNET_DHT_RO_RECORD_ROUTE
We should keep track of the route that the message took in the P2P network.
@ GNUNET_DHT_RO_FIND_PEER
This is a 'FIND-PEER' request, so approximate results are fine.
int GNUNET_CONTAINER_multihashmap_get_multiple(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map that match a particular key.
int GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
int GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
unsigned int GNUNET_CONTAINER_multihashmap_size(const struct GNUNET_CONTAINER_MultiHashMap *map)
Get the number of key-value pairs in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
void * GNUNET_CONTAINER_heap_remove_node(struct GNUNET_CONTAINER_HeapNode *node)
Removes a node from the heap.
struct GNUNET_CONTAINER_HeapNode * GNUNET_CONTAINER_heap_insert(struct GNUNET_CONTAINER_Heap *heap, void *element, GNUNET_CONTAINER_HeapCostType cost)
Inserts a new element into the heap.
void * GNUNET_CONTAINER_heap_peek(const struct GNUNET_CONTAINER_Heap *heap)
Get element stored at the root of heap.
unsigned int GNUNET_CONTAINER_heap_get_size(const struct GNUNET_CONTAINER_Heap *heap)
Get the current size of the heap.
struct GNUNET_CONTAINER_Heap * GNUNET_CONTAINER_heap_create(enum GNUNET_CONTAINER_HeapOrder order)
Create a new heap.
void GNUNET_CONTAINER_heap_destroy(struct GNUNET_CONTAINER_Heap *heap)
Destroys the heap.
@ GNUNET_CONTAINER_HEAP_ORDER_MIN
Heap with the minimum cost at the root.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:94
Information about a block stored in the datacache.
const void * data
Actual block data.
size_t data_size
Number of bytes in data.
const struct GNUNET_DHT_PathElement * put_path
PUT path taken by the block, array of peer identities.
enum GNUNET_BLOCK_Type type
Type of the block.
unsigned int put_path_length
Length of the put_path array.
struct GNUNET_HashCode key
Key of the block.
Block group data.
Handle to a node in a heap.
Internal representation of the hash map.
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
A 512-bit hashcode.
The identity of the host (wraps the signing key of the peer).
Entry for a peer in a bucket.
Closure for the process() function.
const struct GNUNET_DHT_PathElement * get_path
Path of the reply.
const struct GDS_DATACACHE_BlockData * bd
Block data.
unsigned int get_path_length
Number of entries in get_path.
Information we keep about all recent GET requests so that we can route replies.
size_t xquery_size
Number of bytes in xquery.
enum GNUNET_BLOCK_Type type
Type of the requested block.
struct GNUNET_HashCode key
Key of this request.
struct GNUNET_BLOCK_Group * bg
Block group for filtering replies.
struct GNUNET_CONTAINER_HeapNode * heap_node
Position of this node in the min heap.
enum GNUNET_DHT_RouteOption options
Request options.
struct GNUNET_PeerIdentity peer
The peer this request was received from.
const void * xquery
extended query (see gnunet_block_lib.h).
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model