GNUnet 0.22.2
gnunet-service-dht_neighbours.c File Reference

GNUnet DHT service's bucket and neighbour management code. More...

Include dependency graph for gnunet-service-dht_neighbours.c:

Go to the source code of this file.

Data Structures

struct  PeerResultMessage
 P2P Result message. More...
 
struct  PeerGetMessage
 P2P GET message. More...
 
struct  Target
 List of targets that we can use to reach this peer. More...
 
struct  PeerInfo
 Entry for a peer in a bucket. More...
 
struct  PeerBucket
 Peers are grouped into buckets. More...
 

Macros

#define LOG_TRAFFIC(kind, ...)
 
#define SANITY_CHECKS   2
 Enable slow sanity checks to debug issues. More...
 
#define MAX_BUCKETS   sizeof(struct GNUNET_HashCode) * 8
 How many buckets will we allow in total. More...
 
#define DEFAULT_BUCKET_SIZE   8
 What is the maximum number of peers in a given bucket. More...
 
#define FIND_PEER_REPLICATION_LEVEL   4
 Desired replication level for FIND PEER requests. More...
 
#define MAXIMUM_PENDING_PER_PEER   64
 Maximum allowed number of pending messages per peer. More...
 
#define DHT_MINIMUM_FIND_PEER_INTERVAL
 How long at least to wait before sending another find peer request. More...
 
#define DHT_AVG_FIND_PEER_INTERVAL
 How long to additionally wait on average per bucket_size to send out the FIND PEER requests if we did successfully connect (!) to a a new peer and added it to a bucket (as counted in newly_found_peers). More...
 
#define GET_TIMEOUT   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
 How long at most to wait for transmission of a GET request to another peer? More...
 

Functions

static void send_done_cb (void *cls)
 Function called whenever we finished sending to a target. More...
 
static void do_send (struct PeerInfo *pi, const struct GNUNET_MessageHeader *msg)
 Send msg to pi. More...
 
static int find_bucket (const struct GNUNET_HashCode *hc)
 Find the optimal bucket for this key. More...
 
static enum GNUNET_GenericReturnValue add_known_to_bloom (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
 Add each of the peers we already know to the Bloom filter of the request so that we don't get duplicate HELLOs. More...
 
static void send_find_peer_message (void *cls)
 Task to send a find peer message for our own peer identifier so that we can find the closest peers in the network to ourselves and attempt to connect to them. More...
 
static void update_hold (struct PeerBucket *bucket)
 The list of the first bucket_size peers of bucket changed. More...
 
void GDS_u_connect (void *cls, struct GNUNET_DHTU_Target *target, const struct GNUNET_PeerIdentity *pid, void **ctx)
 Function to call when we connect to a peer and can henceforth transmit to that peer. More...
 
void GDS_u_disconnect (void *ctx)
 Function to call when we disconnected from a peer and can henceforth cannot transmit to that peer anymore. More...
 
static unsigned int get_forward_count (uint16_t hop_count, uint16_t target_replication)
 To how many peers should we (on average) forward the request to obtain the desired target_replication count (on average). More...
 
enum GNUNET_GenericReturnValue GDS_am_closest_peer (const struct GNUNET_HashCode *key, const struct GNUNET_CONTAINER_BloomFilter *bloom)
 Check whether my identity is closer than any known peers. More...
 
static struct PeerInfoselect_peer (const struct GNUNET_HashCode *key, const struct GNUNET_CONTAINER_BloomFilter *bloom, uint32_t hops)
 Select a peer from the routing table that would be a good routing destination for sending a message for key. More...
 
static unsigned int get_target_peers (const struct GNUNET_HashCode *key, struct GNUNET_CONTAINER_BloomFilter *bloom, uint16_t hop_count, uint16_t target_replication, struct PeerInfo ***targets)
 Compute the set of peers that the given request should be forwarded to. More...
 
static void hello_check (const struct GNUNET_DATACACHE_Block *bd)
 If we got a HELLO, consider it for our own routing table. More...
 
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_put (const struct GNUNET_DATACACHE_Block *bd, uint16_t desired_replication_level, uint16_t hop_count, struct GNUNET_CONTAINER_BloomFilter *bf)
 Perform a PUT operation. More...
 
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, enum GNUNET_DHT_RouteOption options, uint16_t desired_replication_level, uint16_t hop_count, const struct GNUNET_HashCode *key, const void *xquery, size_t xquery_size, struct GNUNET_BLOCK_Group *bg, struct GNUNET_CONTAINER_BloomFilter *peer_bf)
 Perform a GET operation. More...
 
struct PeerInfoGDS_NEIGHBOURS_lookup_peer (const struct GNUNET_PeerIdentity *target)
 Lookup peer by peer's identity. More...
 
bool GDS_NEIGHBOURS_handle_reply (struct PeerInfo *pi, const struct GNUNET_DATACACHE_Block *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). More...
 
static enum GNUNET_GenericReturnValue check_dht_p2p_put (void *cls, const struct PeerPutMessage *put)
 Check validity of a p2p put request. More...
 
static void handle_dht_p2p_put (void *cls, const struct PeerPutMessage *put)
 Core handler for p2p put requests. More...
 
static void handle_find_my_hello (struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg)
 We have received a request for a HELLO. More...
 
static void handle_find_local_hello (struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg)
 We have received a request for nearby HELLOs. More...
 
static void handle_local_result (void *cls, const struct GNUNET_DATACACHE_Block *bd)
 Handle an exact result from local datacache for a GET operation. More...
 
static enum GNUNET_GenericReturnValue check_dht_p2p_get (void *cls, const struct PeerGetMessage *get)
 Check validity of p2p get request. More...
 
static void handle_dht_p2p_get (void *cls, const struct PeerGetMessage *get)
 Core handler for p2p get requests. More...
 
static bool process_reply_with_path (const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path)
 Process a reply, after the get_path has been updated. More...
 
static enum GNUNET_GenericReturnValue check_dht_p2p_result (void *cls, const struct PeerResultMessage *prm)
 Check validity of p2p result message. More...
 
static void handle_dht_p2p_result (void *cls, const struct PeerResultMessage *prm)
 Core handler for p2p result messages. More...
 
static enum GNUNET_GenericReturnValue check_dht_p2p_hello (void *cls, const struct GNUNET_MessageHeader *hello)
 Check validity of a p2p hello message. More...
 
static void handle_dht_p2p_hello (void *cls, const struct GNUNET_MessageHeader *hello)
 Core handler for p2p HELLO messages. More...
 
void GDS_u_receive (void *cls, void **tctx, void **sctx, const void *message, size_t message_size)
 Function to call when we receive a message. More...
 
void GDS_try_connect (void *cls, const struct GNUNET_PeerIdentity *pid, const char *uri)
 Callback function used to extract URIs from a builder. More...
 
void GDS_NEIGHBOURS_broadcast (const struct GNUNET_MessageHeader *msg)
 Send msg to all peers in our buckets. More...
 
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_init ()
 Initialize neighbours subsystem. More...
 
void GDS_NEIGHBOURS_done ()
 Shutdown neighbours subsystem. More...
 
struct GNUNET_PeerIdentityGDS_NEIGHBOURS_get_id ()
 Get the ID of the local node. More...
 

Variables

static int cache_results
 Do we cache all results that we are routing in the local datacache? More...
 
static unsigned int closest_bucket
 The lowest currently used bucket, initially 0 (for 0-bits matching bucket). More...
 
static unsigned int newly_found_peers
 How many peers have we added since we sent out our last find peer request? More...
 
static int disable_try_connect
 Option for testing that disables the 'connect' function of the DHT. More...
 
static struct PeerBucket k_buckets [sizeof(struct GNUNET_HashCode) *8]
 The buckets. More...
 
static struct GNUNET_CONTAINER_MultiPeerMapall_connected_peers
 Hash map of all CORE-connected peers, for easy removal from k_buckets on disconnect. More...
 
static unsigned int bucket_size = 8
 Maximum size for each bucket. More...
 
static struct GNUNET_SCHEDULER_Taskfind_peer_task
 Task that sends FIND PEER requests. More...
 

Detailed Description

GNUnet DHT service's bucket and neighbour management code.

Author
Christian Grothoff
Nathan Evans

Definition in file gnunet-service-dht_neighbours.c.

Macro Definition Documentation

◆ LOG_TRAFFIC

#define LOG_TRAFFIC (   kind,
  ... 
)
Value:
GNUNET_log_from (kind, "dht-traffic", \
__VA_ARGS__)
#define GNUNET_log_from(kind, comp,...)

Definition at line 37 of file gnunet-service-dht_neighbours.c.

◆ SANITY_CHECKS

#define SANITY_CHECKS   2

Enable slow sanity checks to debug issues.

TODO: might want to eventually implement probabilistic load-based path verification, but for now it is all or nothing based on this define.

0: do not check – if signatures become performance critical 1: check all external inputs – normal production for now 2: check internal computations as well – for debugging

Definition at line 51 of file gnunet-service-dht_neighbours.c.

◆ MAX_BUCKETS

#define MAX_BUCKETS   sizeof(struct GNUNET_HashCode) * 8

How many buckets will we allow in total.

Definition at line 56 of file gnunet-service-dht_neighbours.c.

◆ DEFAULT_BUCKET_SIZE

#define DEFAULT_BUCKET_SIZE   8

What is the maximum number of peers in a given bucket.

Definition at line 61 of file gnunet-service-dht_neighbours.c.

◆ FIND_PEER_REPLICATION_LEVEL

#define FIND_PEER_REPLICATION_LEVEL   4

Desired replication level for FIND PEER requests.

Definition at line 66 of file gnunet-service-dht_neighbours.c.

◆ MAXIMUM_PENDING_PER_PEER

#define MAXIMUM_PENDING_PER_PEER   64

Maximum allowed number of pending messages per peer.

Definition at line 71 of file gnunet-service-dht_neighbours.c.

◆ DHT_MINIMUM_FIND_PEER_INTERVAL

#define DHT_MINIMUM_FIND_PEER_INTERVAL
Value:
#define GNUNET_TIME_UNIT_MINUTES
One minute.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:486

How long at least to wait before sending another find peer request.

This is basically the frequency at which we will usually send out requests when we are 'perfectly' connected.

Definition at line 78 of file gnunet-service-dht_neighbours.c.

◆ DHT_AVG_FIND_PEER_INTERVAL

#define DHT_AVG_FIND_PEER_INTERVAL
Value:
#define GNUNET_TIME_UNIT_SECONDS
One second.

How long to additionally wait on average per bucket_size to send out the FIND PEER requests if we did successfully connect (!) to a a new peer and added it to a bucket (as counted in newly_found_peers).

This time is Multiplied by 100 * newly_found_peers / bucket_size to get the new delay for finding peers (the DHT_MINIMUM_FIND_PEER_INTERVAL is still added on top). Also the range in which we randomize, so the effective value is half of the number given here.

Definition at line 91 of file gnunet-service-dht_neighbours.c.

◆ GET_TIMEOUT

How long at most to wait for transmission of a GET request to another peer?

Definition at line 97 of file gnunet-service-dht_neighbours.c.

Function Documentation

◆ send_done_cb()

static void send_done_cb ( void *  cls)
static

Function called whenever we finished sending to a target.

Marks the transmission as finished (and the target as ready for the next message).

Parameters
clsa struct Target *

Definition at line 398 of file gnunet-service-dht_neighbours.c.

399{
400 struct Target *t = cls;
401 struct PeerInfo *pi = t->pi; /* NULL if t->dropped! */
402
403 GNUNET_assert (t->load > 0);
404 t->load--;
405 if (0 < t->load)
406 return;
407 if (t->dropped)
408 {
409 GNUNET_free (t);
410 return;
411 }
412 /* move target back to the front */
414 pi->t_tail,
415 t);
417 pi->t_tail,
418 t);
419}
static struct GNUNET_SCHEDULER_Task * t
Main task.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_free(ptr)
Wrapper around free.
Entry for a peer in a bucket.
struct Target * t_tail
Tail of DLL of targets for this peer.
struct Target * t_head
Head of DLL of targets for this peer.
List of targets that we can use to reach this peer.

References GNUNET_assert, GNUNET_CONTAINER_DLL_insert, GNUNET_CONTAINER_DLL_remove, GNUNET_free, t, PeerInfo::t_head, and PeerInfo::t_tail.

Referenced by do_send().

Here is the caller graph for this function:

◆ do_send()

static void do_send ( struct PeerInfo pi,
const struct GNUNET_MessageHeader msg 
)
static

Send msg to pi.

Parameters
piwhere to send the message
msgmessage to send

Definition at line 429 of file gnunet-service-dht_neighbours.c.

431{
432 struct Target *t;
433
434 for (t = pi->t_head;
435 NULL != t;
436 t = t->next)
437 if (t->load < MAXIMUM_PENDING_PER_PEER)
438 break;
439 if (NULL == t)
440 {
441 /* all targets busy, drop message */
443 "# messages dropped (underlays busy)",
444 1,
445 GNUNET_NO);
446 return;
447 }
448 t->load++;
449 /* rotate busy targets to the end */
450 if (MAXIMUM_PENDING_PER_PEER == t->load)
451 {
453 pi->t_tail,
454 t);
456 pi->t_tail,
457 t);
458 }
459 GDS_u_send (t->u,
460 t->utarget,
461 msg,
462 ntohs (msg->size),
464 t);
465}
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
void GDS_u_send(struct GDS_Underlay *u, struct GNUNET_DHTU_Target *target, const void *msg, size_t msg_size, GNUNET_SCHEDULER_TaskCallback finished_cb, void *finished_cb_cls)
Send message to some other participant over the network.
struct GNUNET_STATISTICS_Handle * GDS_stats
Handle for the statistics service.
#define MAXIMUM_PENDING_PER_PEER
Maximum allowed number of pending messages per peer.
static void send_done_cb(void *cls)
Function called whenever we finished sending to a target.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
@ GNUNET_NO
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_SCHEDULER_Task * next
This is a linked list.
Definition: scheduler.c:140
struct PeerInfo * pi
Peer this is a target for.

References GDS_stats, GDS_u_send(), GNUNET_CONTAINER_DLL_insert_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_NO, GNUNET_STATISTICS_update(), MAXIMUM_PENDING_PER_PEER, msg, GNUNET_SCHEDULER_Task::next, Target::pi, send_done_cb(), GNUNET_MessageHeader::size, t, PeerInfo::t_head, and PeerInfo::t_tail.

Referenced by GDS_NEIGHBOURS_broadcast(), GDS_NEIGHBOURS_handle_get(), GDS_NEIGHBOURS_handle_put(), and GDS_NEIGHBOURS_handle_reply().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_bucket()

static int find_bucket ( const struct GNUNET_HashCode hc)
static

Find the optimal bucket for this key.

Parameters
hcthe hashcode to compare our identity to
Returns
the proper bucket index, or -1 on error (same hashcode)

Definition at line 476 of file gnunet-service-dht_neighbours.c.

477{
478 struct GNUNET_HashCode xor;
479 unsigned int bits;
480
483 &xor);
485 if (bits == MAX_BUCKETS)
486 {
487 /* How can all bits match? Got my own ID? */
488 GNUNET_break (0);
489 return -1;
490 }
491 return MAX_BUCKETS - bits - 1;
492}
struct GNUNET_HashCode GDS_my_identity_hash
Hash of the identity of this peer.
#define MAX_BUCKETS
How many buckets will we allow in total.
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
Definition: crypto_hash.c:132
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros(const struct GNUNET_HashCode *h)
Count the number of leading 0 bits in h.
Definition: crypto_hash.c:174
uint32_t bits[512/8/sizeof(uint32_t)]
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
A 512-bit hashcode.

References GNUNET_HashCode::bits, GDS_my_identity_hash, GNUNET_break, GNUNET_CRYPTO_hash_count_leading_zeros(), GNUNET_CRYPTO_hash_xor(), and MAX_BUCKETS.

Referenced by GDS_am_closest_peer(), GDS_try_connect(), and GDS_u_connect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_known_to_bloom()

static enum GNUNET_GenericReturnValue add_known_to_bloom ( void *  cls,
const struct GNUNET_PeerIdentity key,
void *  value 
)
static

Add each of the peers we already know to the Bloom filter of the request so that we don't get duplicate HELLOs.

Parameters
clsthe struct GNUNET_BLOCK_Group
keypeer identity to add to the bloom filter
valuethe peer information
Returns
GNUNET_YES (we should continue to iterate)

Definition at line 505 of file gnunet-service-dht_neighbours.c.

508{
509 struct GNUNET_BLOCK_Group *bg = cls;
510 struct PeerInfo *pi = value;
511
513 &pi->phash,
514 1);
516 "Adding known peer (%s) to Bloom filter for FIND PEER\n",
517 GNUNET_i2s (key));
518 return GNUNET_YES;
519}
struct GNUNET_HashCode key
The key used in the DHT.
static char * value
Value of the record to add/remove.
enum GNUNET_GenericReturnValue GNUNET_BLOCK_group_set_seen(struct GNUNET_BLOCK_Group *bg, const struct GNUNET_HashCode *seen_results, unsigned int seen_results_count)
Update block group to filter out the given results.
Definition: block.c:365
#define GNUNET_log(kind,...)
@ GNUNET_YES
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
Block group data.
struct GNUNET_HashCode phash
Hash of id.

References GNUNET_BLOCK_group_set_seen(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), GNUNET_log, GNUNET_YES, key, PeerInfo::phash, and value.

Referenced by send_find_peer_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_find_peer_message()

static void send_find_peer_message ( void *  cls)
static

Task to send a find peer message for our own peer identifier so that we can find the closest peers in the network to ourselves and attempt to connect to them.

Parameters
clsclosure for this task, NULL

Definition at line 530 of file gnunet-service-dht_neighbours.c.

531{
532 (void) cls;
533
534 /* Compute when to do this again (and if we should
535 even send a message right now) */
536 {
537 struct GNUNET_TIME_Relative next_send_time;
538 bool done_early;
539
540 find_peer_task = NULL;
541 done_early = (newly_found_peers > bucket_size);
542 /* schedule next round, taking longer if we found more peers
543 in the last round. */
544 next_send_time.rel_value_us =
550 1 + 100 * (1 + newly_found_peers) / bucket_size).rel_value_us);
554 GNUNET_SCHEDULER_add_delayed (next_send_time,
556 NULL);
557 if (done_early)
558 return;
559 }
560
561 /* actually send 'find peer' request */
562 {
563 struct GNUNET_BLOCK_Group *bg;
564 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
565
568 NULL,
569 0,
570 "seen-set-size",
573 NULL);
576 bg);
577 peer_bf
581 if (GNUNET_OK !=
586 0, /* hop count */
588 NULL, 0, /* xquery */
589 bg,
590 peer_bf))
591 {
593 "# Failed to initiate FIND PEER lookup",
594 1,
595 GNUNET_NO);
596 }
597 else
598 {
600 "# FIND PEER messages initiated",
601 1,
602 GNUNET_NO);
603 }
606 }
607}
#define DHT_BLOOM_SIZE
Size of the bloom filter the DHT uses to filter peers.
Definition: dht.h:34
struct GNUNET_BLOCK_Context * GDS_block_context
Our handle to the BLOCK library.
static void send_find_peer_message(void *cls)
Task to send a find peer message for our own peer identifier so that we can find the closest peers in...
static unsigned int newly_found_peers
How many peers have we added since we sent out our last find peer request?
#define FIND_PEER_REPLICATION_LEVEL
Desired replication level for FIND PEER requests.
static struct GNUNET_CONTAINER_MultiPeerMap * all_connected_peers
Hash map of all CORE-connected peers, for easy removal from k_buckets on disconnect.
#define DHT_MINIMUM_FIND_PEER_INTERVAL
How long at least to wait before sending another find peer request.
static struct GNUNET_SCHEDULER_Task * find_peer_task
Task that sends FIND PEER requests.
static enum GNUNET_GenericReturnValue add_known_to_bloom(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Add each of the peers we already know to the Bloom filter of the request so that we don't get duplica...
static unsigned int bucket_size
Maximum size for each bucket.
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_get(enum GNUNET_BLOCK_Type type, enum GNUNET_DHT_RouteOption options, uint16_t desired_replication_level, uint16_t hop_count, const struct GNUNET_HashCode *key, const void *xquery, size_t xquery_size, struct GNUNET_BLOCK_Group *bg, struct GNUNET_CONTAINER_BloomFilter *peer_bf)
Perform a GET operation.
#define DHT_AVG_FIND_PEER_INTERVAL
How long to additionally wait on average per bucket_size to send out the FIND PEER requests if we did...
@ GNUNET_BLOCK_TYPE_DHT_HELLO
Type of a block that contains a DHT-NG HELLO for a peer.
void GNUNET_BLOCK_group_destroy(struct GNUNET_BLOCK_Group *bg)
Destroy resources used by a block group.
Definition: block.c:194
struct GNUNET_BLOCK_Group * GNUNET_BLOCK_group_create(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *raw_data, size_t raw_data_size,...)
Create a new block group.
Definition: block.c:249
struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_init(const char *data, size_t size, unsigned int k)
Create a Bloom filter from raw bits.
void GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf)
Free the space associated with a filter in memory, flush to drive if needed (do not free the space on...
#define GNUNET_CONSTANTS_BLOOMFILTER_K
K-value that must be used for the bloom filters in 'GET' queries.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
@ GNUNET_DHT_RO_RECORD_ROUTE
We should keep track of the route that the message took in the P2P network.
@ GNUNET_DHT_RO_FIND_APPROXIMATE
Approximate results are fine.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
@ GNUNET_OK
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1277
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.

References add_known_to_bloom(), all_connected_peers, bucket_size, DHT_AVG_FIND_PEER_INTERVAL, DHT_BLOOM_SIZE, DHT_MINIMUM_FIND_PEER_INTERVAL, FIND_PEER_REPLICATION_LEVEL, find_peer_task, GDS_block_context, GDS_my_identity_hash, GDS_NEIGHBOURS_handle_get(), GDS_stats, GNUNET_assert, GNUNET_BLOCK_group_create(), GNUNET_BLOCK_group_destroy(), GNUNET_BLOCK_TYPE_DHT_HELLO, GNUNET_CONSTANTS_BLOOMFILTER_K, GNUNET_CONTAINER_bloomfilter_free(), GNUNET_CONTAINER_bloomfilter_init(), GNUNET_CONTAINER_multipeermap_iterate(), GNUNET_CONTAINER_multipeermap_size(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_DHT_RO_FIND_APPROXIMATE, GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_STATISTICS_update(), GNUNET_TIME_relative_multiply(), newly_found_peers, GNUNET_TIME_Relative::rel_value_us, and send_find_peer_message().

Referenced by GDS_u_connect(), and send_find_peer_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_hold()

static void update_hold ( struct PeerBucket bucket)
static

The list of the first bucket_size peers of bucket changed.

We should thus make sure we have called 'hold' all of the first bucket_size peers!

Parameters
[in,out]bucketthe bucket where the peer set changed

Definition at line 618 of file gnunet-service-dht_neighbours.c.

619{
620 unsigned int off = 0;
621
622 /* find the peer -- we just go over all of them, should
623 be hardly any more expensive than just finding the 'right'
624 one. */
625 for (struct PeerInfo *pos = bucket->head;
626 NULL != pos;
627 pos = pos->next)
628 {
629 if (off > bucket_size)
630 break; /* We only hold up to #bucket_size peers per bucket */
631 off++;
632 for (struct Target *tp = pos->t_head;
633 NULL != tp;
634 tp = tp->next)
635 if (NULL == tp->ph)
636 tp->ph = GDS_u_hold (tp->u,
637 tp->utarget);
638 }
639}
struct GNUNET_DHTU_PreferenceHandle * GDS_u_hold(struct GDS_Underlay *u, struct GNUNET_DHTU_Target *target)
Create a hold on target at underlay u.
struct PeerInfo * head
Head of DLL.
struct PeerInfo * next
Next peer entry (DLL)
struct Target * next
Kept in a DLL.

References bucket_size, GDS_u_hold(), PeerBucket::head, Target::next, and PeerInfo::next.

Referenced by GDS_u_connect(), and GDS_u_disconnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_u_connect()

void GDS_u_connect ( void *  cls,
struct GNUNET_DHTU_Target target,
const struct GNUNET_PeerIdentity pid,
void **  ctx 
)

Function to call when we connect to a peer and can henceforth transmit to that peer.

Parameters
clsthe closure, must be a struct GDS_Underlay
targethandle to the target, pointer will remain valid until disconnect_cb is called
pidpeer identity, pointer will remain valid until disconnect_cb is called
[out]ctxstorage space for DHT to use in association with this target

Definition at line 643 of file gnunet-service-dht_neighbours.c.

647{
648 struct GDS_Underlay *u = cls;
649 struct PeerInfo *pi;
650 struct PeerBucket *bucket;
651 bool do_hold = false;
652
653 /* Check for connect to self message */
655 pid))
656 return;
658 "Connected to peer %s\n",
659 GNUNET_i2s (pid));
661 pid);
662 if (NULL == pi)
663 {
665 "# peers connected",
666 1,
667 GNUNET_NO);
668 pi = GNUNET_new (struct PeerInfo);
669 pi->id = *pid;
671 sizeof(*pid),
672 &pi->phash);
673 pi->peer_bucket = find_bucket (&pi->phash);
674 GNUNET_assert ( (pi->peer_bucket >= 0) &&
675 ((unsigned int) pi->peer_bucket < MAX_BUCKETS));
676 bucket = &k_buckets[pi->peer_bucket];
678 bucket->tail,
679 pi);
680 bucket->peers_size++;
682 (unsigned int) pi->peer_bucket + 1);
685 &pi->id,
686 pi,
688 if (bucket->peers_size <= bucket_size)
689 {
691 do_hold = true;
692 }
695 {
696 /* got a first connection, good time to start with FIND PEER requests... */
699 NULL);
700 }
701 }
702 {
703 struct Target *t;
704
705 t = GNUNET_new (struct Target);
706 t->u = u;
707 t->utarget = target;
708 t->pi = pi;
710 pi->t_tail,
711 t);
712 *ctx = t;
713
714 }
715 if (do_hold)
716 update_hold (bucket);
717}
static mp_limb_t u[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
static struct GNUNET_FS_Handle * ctx
struct GNUNET_PeerIdentity GDS_my_identity
Identity of this peer.
static int disable_try_connect
Option for testing that disables the 'connect' function of the DHT.
static int find_bucket(const struct GNUNET_HashCode *hc)
Find the optimal bucket for this key.
static unsigned int closest_bucket
The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
static struct PeerBucket k_buckets[sizeof(struct GNUNET_HashCode) *8]
The buckets.
static void update_hold(struct PeerBucket *bucket)
The list of the first bucket_size peers of bucket changed.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
#define GNUNET_MAX(a, b)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1304
Information we keep per underlay.
Peers are grouped into buckets.
struct PeerInfo * tail
Tail of DLL.
unsigned int peers_size
Number of peers in the bucket.
struct GNUNET_PeerIdentity id
What is the identity of the peer?
int peer_bucket
Which bucket is this peer in?

References all_connected_peers, bucket_size, closest_bucket, ctx, disable_try_connect, find_bucket(), find_peer_task, GDS_my_identity, GDS_stats, GNUNET_assert, GNUNET_CONTAINER_DLL_insert, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CONTAINER_multipeermap_get(), GNUNET_CONTAINER_multipeermap_put(), GNUNET_CONTAINER_multipeermap_size(), GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), GNUNET_log, GNUNET_MAX, GNUNET_memcmp, GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_now(), GNUNET_STATISTICS_update(), GNUNET_YES, PeerBucket::head, PeerInfo::id, k_buckets, MAX_BUCKETS, newly_found_peers, PeerInfo::peer_bucket, PeerBucket::peers_size, PeerInfo::phash, Target::pi, pid, send_find_peer_message(), t, PeerInfo::t_head, PeerInfo::t_tail, PeerBucket::tail, u, and update_hold().

Referenced by load_underlay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_u_disconnect()

void GDS_u_disconnect ( void *  ctx)

Function to call when we disconnected from a peer and can henceforth cannot transmit to that peer anymore.

Parameters
[in]ctxstorage space used by the DHT in association with this target

Definition at line 721 of file gnunet-service-dht_neighbours.c.

722{
723 struct Target *t = ctx;
724 struct PeerInfo *pi;
725 struct PeerBucket *bucket;
726 bool was_held = false;
727
728 /* Check for disconnect from self message (on shutdown) */
729 if (NULL == t)
730 return;
731 pi = t->pi;
733 pi->t_tail,
734 t);
735 if (NULL != t->ph)
736 {
737 GDS_u_drop (t->u,
738 t->ph);
739 t->ph = NULL;
740 was_held = true;
741 }
742 if (t->load > 0)
743 {
744 t->dropped = true;
745 t->pi = NULL;
746 }
747 else
748 {
749 GNUNET_free (t);
750 }
751 if (NULL != pi->t_head)
752 return; /* got other connections still */
754 "Disconnected from peer %s\n",
755 GNUNET_i2s (&pi->id));
757 "# peers connected",
758 -1,
759 GNUNET_NO);
762 &pi->id,
763 pi));
766 {
768 find_peer_task = NULL;
769 }
770 GNUNET_assert (pi->peer_bucket >= 0);
771 bucket = &k_buckets[pi->peer_bucket];
773 bucket->tail,
774 pi);
775 GNUNET_assert (bucket->peers_size > 0);
776 bucket->peers_size--;
777 if ( (was_held) &&
778 (bucket->peers_size >= bucket_size - 1) )
779 update_hold (bucket);
780 while ( (closest_bucket > 0) &&
783 GNUNET_free (pi->hello);
784 GNUNET_free (pi);
785}
void GDS_u_drop(struct GDS_Underlay *u, struct GNUNET_DHTU_PreferenceHandle *ph)
Drop a hold ph from underlay u.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980
void * hello
Block with a HELLO of this peer.

References all_connected_peers, bucket_size, closest_bucket, ctx, disable_try_connect, find_peer_task, GDS_stats, GDS_u_drop(), GNUNET_assert, GNUNET_CONTAINER_DLL_remove, GNUNET_CONTAINER_multipeermap_remove(), GNUNET_CONTAINER_multipeermap_size(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_i2s(), GNUNET_log, GNUNET_NO, GNUNET_SCHEDULER_cancel(), GNUNET_STATISTICS_update(), GNUNET_YES, PeerBucket::head, PeerInfo::hello, PeerInfo::id, k_buckets, PeerInfo::peer_bucket, PeerBucket::peers_size, t, PeerInfo::t_head, PeerInfo::t_tail, PeerBucket::tail, and update_hold().

Referenced by load_underlay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_forward_count()

static unsigned int get_forward_count ( uint16_t  hop_count,
uint16_t  target_replication 
)
static

To how many peers should we (on average) forward the request to obtain the desired target_replication count (on average).

Parameters
hop_countnumber of hops the message has traversed
target_replicationthe number of total paths desired
Returns
Some number of peers to forward the message to

Definition at line 797 of file gnunet-service-dht_neighbours.c.

799{
800 uint32_t random_value;
801 uint32_t forward_count;
802 float target_value;
803 double rm1;
804
805 if (hop_count > GDS_NSE_get () * 4.0)
806 {
807 /* forcefully terminate */
809 "# requests TTL-dropped",
810 1,
811 GNUNET_NO);
812 return 0;
813 }
814 if (hop_count > GDS_NSE_get () * 2.0)
815 {
816 /* Once we have reached our ideal number of hops, only forward to 1 peer */
817 return 1;
818 }
819 /* bound by system-wide maximum and minimum */
820 if (0 == target_replication)
821 target_replication = 1; /* 0 is verboten */
822 target_replication =
824 target_replication);
825 rm1 = target_replication - 1.0;
826 target_value =
827 1 + (rm1) / (GDS_NSE_get () + (rm1 * hop_count));
828
829 /* Set forward count to floor of target_value */
830 forward_count = (uint32_t) target_value;
831 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
832 target_value = target_value - forward_count;
834 UINT32_MAX);
835 if (random_value < (target_value * UINT32_MAX))
836 forward_count++;
837 return GNUNET_MIN (forward_count,
839}
double GDS_NSE_get(void)
Return the current NSE.
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
#define GNUNET_DHT_MAXIMUM_REPLICATION_LEVEL
Maximum allowed replication level for all requests.
#define GNUNET_MIN(a, b)

References GDS_NSE_get(), GDS_stats, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u32(), GNUNET_DHT_MAXIMUM_REPLICATION_LEVEL, GNUNET_MIN, GNUNET_NO, and GNUNET_STATISTICS_update().

Referenced by get_target_peers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_am_closest_peer()

enum GNUNET_GenericReturnValue GDS_am_closest_peer ( const struct GNUNET_HashCode key,
const struct GNUNET_CONTAINER_BloomFilter bloom 
)

Check whether my identity is closer than any known peers.

If a non-null bloomfilter is given, check if this is the closest peer that hasn't already been routed to.

Parameters
keyhash code to check closeness to
bloombloomfilter, exclude these entries from the decision
Returns
GNUNET_YES if node location is closest, GNUNET_NO otherwise.

Definition at line 853 of file gnunet-service-dht_neighbours.c.

855{
856 int delta;
858 key))
859 return GNUNET_YES;
860 for (int bucket_num = find_bucket (key);
861 bucket_num < closest_bucket;
862 bucket_num++)
863 {
864 unsigned int count = 0;
865
866 GNUNET_assert (bucket_num >= 0);
867 for (struct PeerInfo *pos = k_buckets[bucket_num].head;
868 NULL != pos;
869 pos = pos->next)
870 {
871 if (count >= bucket_size)
872 break; /* we only consider first #bucket_size entries per bucket */
873 count++;
874 if ( (NULL != bloom) &&
875 (GNUNET_YES ==
877 &pos->phash)) )
878 continue; /* Ignore filtered peers */
879 /* All peers in this bucket must be closer than us, as
880 they mismatch with our PID on the pivotal bit. So
881 because an unfiltered peer exists, we are not the
882 closest. */
883 delta = GNUNET_CRYPTO_hash_xorcmp (&pos->phash,
885 key);
886 switch (delta)
887 {
888 case -1: /* pos closer */
889 return GNUNET_NO;
890 case 0: /* identical, impossible! */
891 GNUNET_assert (0);
892 break;
893 case 1: /* I am closer */
894 break;
895 }
896 }
897 }
898 /* No closer (unfiltered) peers found; we must be the closest! */
899 return GNUNET_YES;
900}
bool GNUNET_CONTAINER_bloomfilter_test(const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Test if an element is in the filter.
int GNUNET_CRYPTO_hash_xorcmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2, const struct GNUNET_HashCode *target)
Find out which of the two GNUNET_CRYPTO_hash codes is closer to target in the XOR metric (Kademlia).
Definition: crypto_hash.c:240
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:36

References bucket_size, closest_bucket, delta, find_bucket(), GDS_my_identity_hash, GNUNET_assert, GNUNET_CONTAINER_bloomfilter_test(), GNUNET_CRYPTO_hash_xorcmp(), GNUNET_memcmp, GNUNET_NO, GNUNET_YES, PeerBucket::head, k_buckets, key, and PeerInfo::next.

Referenced by handle_dht_local_put(), handle_dht_p2p_get(), and handle_dht_p2p_put().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ select_peer()

static struct PeerInfo * select_peer ( const struct GNUNET_HashCode key,
const struct GNUNET_CONTAINER_BloomFilter bloom,
uint32_t  hops 
)
static

Select a peer from the routing table that would be a good routing destination for sending a message for key.

The resulting peer must not be in the set of bloom blocked peers.

Note that we should not ALWAYS select the closest peer to the target, we do a "random" peer selection if the number of hops is below the logarithm of the network size estimate.

In all cases, we only consider at most the first bucket_size peers of any k_buckets. The other peers in the bucket are there because GNUnet doesn't really allow the DHT to "reject" connections, but we only use the first bucket_size, even if more exist. (The idea is to ensure that those connections are frequently used, and for others to be not used by the DHT, and thus possibly dropped by transport due to disuse).

Parameters
keythe key we are selecting a peer to route to
blooma Bloom filter containing entries this request has seen already
hopshow many hops has this message traversed thus far
Returns
Peer to route to, or NULL on error

Definition at line 925 of file gnunet-service-dht_neighbours.c.

928{
929 if (0 == closest_bucket)
930 {
932 "# Peer selection failed",
933 1,
934 GNUNET_NO);
935 return NULL; /* we have zero connections */
936 }
937 if (hops >= GDS_NSE_get ())
938 {
939 /* greedy selection (closest peer that is not in Bloom filter) */
940 struct PeerInfo *chosen = NULL;
941 int best_bucket;
942 int bucket_offset;
943
944 {
945 struct GNUNET_HashCode xor;
946
949 &xor);
950 best_bucket = GNUNET_CRYPTO_hash_count_leading_zeros (&xor);
951 }
952 if (best_bucket >= closest_bucket)
953 bucket_offset = closest_bucket - 1;
954 else
955 bucket_offset = best_bucket;
956 while (-1 != bucket_offset)
957 {
958 struct PeerBucket *bucket = &k_buckets[bucket_offset];
959 unsigned int count = 0;
960
961 for (struct PeerInfo *pos = bucket->head;
962 NULL != pos;
963 pos = pos->next)
964 {
965 if (count >= bucket_size)
966 break; /* we only consider first #bucket_size entries per bucket */
967 count++;
968 if ( (NULL != bloom) &&
969 (GNUNET_YES ==
971 &pos->phash)) )
972 {
974 "Excluded peer `%s' due to BF match in greedy routing for %s\n",
975 GNUNET_i2s (&pos->id),
976 GNUNET_h2s (key));
977 continue;
978 }
979 if (NULL == chosen)
980 {
981 /* First candidate */
982 chosen = pos;
983 }
984 else
985 {
986 int delta = GNUNET_CRYPTO_hash_xorcmp (&pos->phash,
987 &chosen->phash,
988 key);
989 switch (delta)
990 {
991 case -1: /* pos closer */
992 chosen = pos;
993 break;
994 case 0: /* identical, impossible! */
995 GNUNET_assert (0);
996 break;
997 case 1: /* chosen closer */
998 break;
999 }
1000 }
1001 count++;
1002 } /* for all (#bucket_size) peers in bucket */
1003 if (NULL != chosen)
1004 break;
1005
1006 /* If we chose nothing in first iteration, first go through deeper
1007 buckets (best chance to find a good match), and if we still found
1008 nothing, then to shallower buckets. Terminate on any match in the
1009 current bucket, as this search order guarantees that it can only get
1010 worse as we keep going. */
1011 if (bucket_offset > best_bucket)
1012 {
1013 /* Go through more deeper buckets */
1014 bucket_offset++;
1015 if (bucket_offset == closest_bucket)
1016 {
1017 /* Can't go any deeper, if nothing selected,
1018 go for shallower buckets */
1019 bucket_offset = best_bucket - 1;
1020 }
1021 }
1022 else
1023 {
1024 /* We're either at the 'best_bucket' or already moving
1025 on to shallower buckets. */
1026 if (bucket_offset == best_bucket)
1027 bucket_offset++; /* go for deeper buckets */
1028 else
1029 bucket_offset--; /* go for shallower buckets */
1030 }
1031 } /* for applicable buckets (starting at best match) */
1032 if (NULL == chosen)
1033 {
1035 "# Peer selection failed",
1036 1,
1037 GNUNET_NO);
1038 return NULL;
1039 }
1041 "Selected peer `%s' in greedy routing for %s\n",
1042 GNUNET_i2s (&chosen->id),
1043 GNUNET_h2s (key));
1044 return chosen;
1045 } /* end of 'greedy' peer selection */
1046
1047 /* select "random" peer */
1048 /* count number of peers that are available and not filtered,
1049 but limit to at most #bucket_size peers, starting with
1050 those 'furthest' from us. */
1051 {
1052 unsigned int total = 0;
1053 unsigned int selected;
1054
1055 for (unsigned int bc = 0; bc < closest_bucket; bc++)
1056 {
1057 struct PeerBucket *bucket = &k_buckets[bc];
1058 unsigned int count = 0;
1059
1060 for (struct PeerInfo *pos = bucket->head;
1061 NULL != pos;
1062 pos = pos->next)
1063 {
1064 count++;
1065 if (count > bucket_size)
1066 break; /* limits search to #bucket_size peers per bucket */
1067 if ( (NULL != bloom) &&
1068 (GNUNET_YES ==
1070 &pos->phash)) )
1071 {
1073 "Excluded peer `%s' due to BF match in random routing for %s\n",
1074 GNUNET_i2s (&pos->id),
1075 GNUNET_h2s (key));
1076 continue; /* Ignore filtered peers */
1077 }
1078 total++;
1079 } /* for all peers in bucket */
1080 } /* for all buckets */
1081 if (0 == total) /* No peers to select from! */
1082 {
1084 "# Peer selection failed",
1085 1,
1086 GNUNET_NO);
1087 return NULL;
1088 }
1089
1090 /* Now actually choose a peer */
1092 total);
1093 for (unsigned int bc = 0; bc < closest_bucket; bc++)
1094 {
1095 unsigned int count = 0;
1096
1097 for (struct PeerInfo *pos = k_buckets[bc].head;
1098 pos != NULL;
1099 pos = pos->next)
1100 {
1101 count++;
1102 if (count > bucket_size)
1103 break; /* limits search to #bucket_size peers per bucket */
1104
1105 if ( (NULL != bloom) &&
1106 (GNUNET_YES ==
1108 &pos->phash)) )
1109 continue; /* Ignore bloomfiltered peers */
1110 if (0 == selected--)
1111 {
1113 "Selected peer `%s' in random routing for %s\n",
1114 GNUNET_i2s (&pos->id),
1115 GNUNET_h2s (key));
1116 return pos;
1117 }
1118 } /* for peers in bucket */
1119 } /* for all buckets */
1120 } /* random peer selection scope */
1121 GNUNET_break (0);
1122 return NULL;
1123}
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).

References bucket_size, closest_bucket, delta, GDS_my_identity_hash, GDS_NSE_get(), GDS_stats, GNUNET_assert, GNUNET_break, GNUNET_CONTAINER_bloomfilter_test(), GNUNET_CRYPTO_hash_count_leading_zeros(), GNUNET_CRYPTO_hash_xor(), GNUNET_CRYPTO_hash_xorcmp(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u32(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s(), GNUNET_i2s(), GNUNET_log, GNUNET_NO, GNUNET_STATISTICS_update(), GNUNET_YES, PeerBucket::head, PeerInfo::id, k_buckets, key, PeerInfo::next, and PeerInfo::phash.

Referenced by get_target_peers(), and handle_find_local_hello().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_target_peers()

static unsigned int get_target_peers ( const struct GNUNET_HashCode key,
struct GNUNET_CONTAINER_BloomFilter bloom,
uint16_t  hop_count,
uint16_t  target_replication,
struct PeerInfo ***  targets 
)
static

Compute the set of peers that the given request should be forwarded to.

Parameters
keyrouting key
[in,out]bloomBloom filter excluding peers as targets, all selected peers will be added to the Bloom filter
hop_countnumber of hops the request has traversed so far
target_replicationdesired number of replicas
[out]targetswhere to store an array of target peers (to be free()ed by the caller)
Returns
number of peers returned in targets.

Definition at line 1140 of file gnunet-service-dht_neighbours.c.

1145{
1146 unsigned int target;
1147 unsigned int off;
1148 struct PeerInfo **rtargets;
1149
1150 GNUNET_assert (NULL != bloom);
1151 target = get_forward_count (hop_count,
1152 target_replication);
1153 if (0 == target)
1154 {
1155 *targets = NULL;
1156 return 0;
1157 }
1158 rtargets = GNUNET_new_array (target,
1159 struct PeerInfo *);
1160 for (off = 0; off < target; off++)
1161 {
1162 struct PeerInfo *nxt;
1163
1164 nxt = select_peer (key,
1165 bloom,
1166 hop_count);
1167 if (NULL == nxt)
1168 break;
1169 rtargets[off] = nxt;
1170 }
1172 "Selected %u/%u peers at hop %u for %s (target was %u)\n",
1173 off,
1175 (unsigned int) hop_count,
1176 GNUNET_h2s (key),
1177 target);
1178 if (0 == off)
1179 {
1180 GNUNET_free (rtargets);
1181 *targets = NULL;
1182 return 0;
1183 }
1184 *targets = rtargets;
1186 "Forwarding query `%s' to %u peers (goal was %u peers)\n",
1187 GNUNET_h2s (key),
1188 off,
1189 target);
1190 return off;
1191}
static struct PeerInfo * select_peer(const struct GNUNET_HashCode *key, const struct GNUNET_CONTAINER_BloomFilter *bloom, uint32_t hops)
Select a peer from the routing table that would be a good routing destination for sending a message f...
static unsigned int get_forward_count(uint16_t hop_count, uint16_t target_replication)
To how many peers should we (on average) forward the request to obtain the desired target_replication...
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.

References all_connected_peers, get_forward_count(), GNUNET_assert, GNUNET_CONTAINER_multipeermap_size(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_h2s(), GNUNET_log, GNUNET_new_array, key, and select_peer().

Referenced by GDS_NEIGHBOURS_handle_get(), and GDS_NEIGHBOURS_handle_put().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hello_check()

static void hello_check ( const struct GNUNET_DATACACHE_Block bd)
static

If we got a HELLO, consider it for our own routing table.

Parameters
bdblock data we got

Definition at line 1200 of file gnunet-service-dht_neighbours.c.

1201{
1202 struct GNUNET_HELLO_Parser *b;
1203
1205 return;
1206
1208 bd->data_size);
1210 {
1213 NULL);
1214 }
1216}
void GDS_try_connect(void *cls, const struct GNUNET_PeerIdentity *pid, const char *uri)
Callback function used to extract URIs from a builder.
void GNUNET_HELLO_parser_free(struct GNUNET_HELLO_Parser *parser)
Release resources of a builder.
Definition: hello-uri.c:435
const struct GNUNET_PeerIdentity * GNUNET_HELLO_parser_iterate(const struct GNUNET_HELLO_Parser *parser, GNUNET_HELLO_UriCallback uc, void *uc_cls)
Iterate over URIs in a parser.
Definition: hello-uri.c:1201
struct GNUNET_HELLO_Parser * GNUNET_HELLO_parser_from_block(const void *block, size_t block_size)
Parse block.
Definition: hello-uri.c:559
enum GNUNET_BLOCK_Type type
Type of the block.
const void * data
Actual block data.
size_t data_size
Number of bytes in data.
Context for parsing HELLOs.
Definition: hello-uri.c:258

References GNUNET_DATACACHE_Block::data, GNUNET_DATACACHE_Block::data_size, disable_try_connect, GDS_try_connect(), GNUNET_BLOCK_TYPE_DHT_HELLO, GNUNET_HELLO_parser_free(), GNUNET_HELLO_parser_from_block(), GNUNET_HELLO_parser_iterate(), GNUNET_YES, and GNUNET_DATACACHE_Block::type.

Referenced by GDS_NEIGHBOURS_handle_put(), and handle_dht_p2p_result().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_NEIGHBOURS_handle_put()

enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_put ( const struct GNUNET_DATACACHE_Block bd,
uint16_t  desired_replication_level,
uint16_t  hop_count,
struct GNUNET_CONTAINER_BloomFilter bf 
)

Perform a PUT operation.

Forwards the given request to other peers. Does not store the data locally. Does not give the data to local clients. May do nothing if this is the only peer in the network (or if we are the closest peer in the network).

Parameters
bddata about the block
desired_replication_leveldesired replication level
hop_counthow many hops has this message traversed so far
bfBloom filter of peers this PUT has already traversed
Returns
GNUNET_OK if the request was forwarded, GNUNET_NO if not

Definition at line 1220 of file gnunet-service-dht_neighbours.c.

1224{
1225 unsigned int target_count;
1226 struct PeerInfo **targets;
1227 size_t msize;
1228 enum GNUNET_DHT_RouteOption ro = bd->ro;
1229 unsigned int put_path_length = bd->put_path_length;
1230 const struct GNUNET_DHT_PathElement *put_path = bd->put_path;
1231 bool truncated = (0 != (bd->ro & GNUNET_DHT_RO_TRUNCATED));
1232 const struct GNUNET_PeerIdentity *trunc_peer
1233 = truncated
1234 ? &bd->trunc_peer
1235 : NULL;
1236 struct GNUNET_PeerIdentity trunc_peer_out;
1238
1241 bd->ro, &ro,
1242 bd->expiration_time,
1243 bd->data, bd->data_size,
1244 put_path, put_path_length,
1245 &put_path_length,
1246 trunc_peer,
1247 &trunc_peer_out,
1248 &truncated);
1249 if (truncated)
1250 trunc_peer = &trunc_peer_out;
1251 /* Path may have been truncated by the call above */
1253 "Adding myself (%s) to PUT bloomfilter for %s with RO(%s/%s)\n",
1255 GNUNET_h2s (&bd->key),
1256 (bd->ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE) ? "x" : "-",
1257 (bd->ro & GNUNET_DHT_RO_RECORD_ROUTE) ? "R" : "-");
1258
1259 /* if we got a HELLO, consider it for our own routing table */
1260 hello_check (bd);
1261 GNUNET_assert (NULL != bf);
1265 "# PUT requests routed",
1266 1,
1267 GNUNET_NO);
1268 if (GNUNET_OK != ret)
1269 return ret;
1270 target_count
1271 = get_target_peers (&bd->key,
1272 bf,
1273 hop_count,
1274 desired_replication_level,
1275 &targets);
1276 if (0 == target_count)
1277 {
1279 "Routing PUT for %s terminates after %u hops at %s\n",
1280 GNUNET_h2s (&bd->key),
1281 (unsigned int) hop_count,
1283 return GNUNET_NO;
1284 }
1285 for (unsigned int i = 0; i < target_count; i++)
1286 {
1287 struct PeerInfo *target = targets[i];
1288
1290 &target->phash);
1291 }
1292 for (unsigned int i = 0; i < target_count; i++)
1293 {
1294 struct PeerInfo *target = targets[i];
1295 struct PeerPutMessage *ppm;
1296 char buf[msize] GNUNET_ALIGN;
1297
1298 ppm = (struct PeerPutMessage *) buf;
1299 GDS_helper_make_put_message (ppm, msize,
1301 &target->id,
1302 &target->phash,
1303 bf,
1304 &bd->key,
1305 ro,
1306 bd->type,
1307 bd->expiration_time,
1308 bd->data, bd->data_size,
1309 put_path, put_path_length,
1310 hop_count,
1312 trunc_peer);
1314 "Routing PUT for %s after %u hops to %s\n",
1315 GNUNET_h2s (&bd->key),
1316 (unsigned int) hop_count,
1317 GNUNET_i2s (&target->id));
1318 do_send (target,
1319 &ppm->header);
1320 }
1321 GNUNET_free (targets);
1323 "# PUT messages queued for transmission",
1324 target_count,
1325 GNUNET_NO);
1326 return GNUNET_OK;
1327}
void GDS_helper_make_put_message(struct PeerPutMessage *ppm, size_t msize, const struct GNUNET_CRYPTO_EddsaPrivateKey *sk, const struct GNUNET_PeerIdentity *target, const struct GNUNET_HashCode *target_hash, const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *block_key, enum GNUNET_DHT_RouteOption ro, enum GNUNET_BLOCK_Type block_type, struct GNUNET_TIME_Absolute block_expiration_time, const uint8_t *block_data, size_t block_data_len, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_len, size_t hop_count, uint32_t desired_replication_level, const struct GNUNET_PeerIdentity *trunc_peer)
Definition: dht_helper.c:178
enum GNUNET_GenericReturnValue GDS_helper_put_message_get_size(size_t *msize_out, const struct GNUNET_PeerIdentity *my_identity, enum GNUNET_DHT_RouteOption ro_in, enum GNUNET_DHT_RouteOption *ro_out, struct GNUNET_TIME_Absolute block_expiration_time, const uint8_t *block_data, size_t block_data_len, const struct GNUNET_DHT_PathElement *put_path_in, unsigned int put_path_len_in, unsigned int *put_path_len_out, const struct GNUNET_PeerIdentity *trunc_peer, struct GNUNET_PeerIdentity *trunc_peer_out, bool *truncated)
Definition: dht_helper.c:36
static int ret
Final status code.
Definition: gnunet-arm.c:93
struct GNUNET_CRYPTO_EddsaPrivateKey GDS_my_private_key
Our private key.
static void do_send(struct PeerInfo *pi, const struct GNUNET_MessageHeader *msg)
Send msg to pi.
static unsigned int get_target_peers(const struct GNUNET_HashCode *key, struct GNUNET_CONTAINER_BloomFilter *bloom, uint16_t hop_count, uint16_t target_replication, struct PeerInfo ***targets)
Compute the set of peers that the given request should be forwarded to.
static void hello_check(const struct GNUNET_DATACACHE_Block *bd)
If we got a HELLO, consider it for our own routing table.
void GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Add an element to the filter.
GNUNET_DHT_RouteOption
Options for routing.
@ GNUNET_DHT_RO_TRUNCATED
Flag set if the path was truncated.
@ GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
Each peer along the way should process the request (otherwise only peers locally closest to the key w...
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
GNUNET_GenericReturnValue
Named constants for return values.
const struct GNUNET_DHT_PathElement * put_path
PUT path taken by the block, array of peer identities.
enum GNUNET_DHT_RouteOption ro
Options for routing for the block.
struct GNUNET_PeerIdentity trunc_peer
If the path was truncated, this is the peer ID at which the path was truncated.
struct GNUNET_HashCode key
Key of the block.
unsigned int put_path_length
Length of the put_path array.
struct GNUNET_TIME_Absolute expiration_time
When does the block expire?
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
The identity of the host (wraps the signing key of the peer).
P2P PUT message.
Definition: dht.h:429
uint16_t desired_replication_level
Replication level for this message.
Definition: dht.h:453
uint16_t hop_count
Hop count.
Definition: dht.h:448
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_DHT_P2P_PUT.
Definition: dht.h:433
uint16_t put_path_length
Length of the PUT path that follows (if tracked).
Definition: dht.h:458

References GNUNET_DATACACHE_Block::data, GNUNET_DATACACHE_Block::data_size, PeerPutMessage::desired_replication_level, do_send(), GNUNET_DATACACHE_Block::expiration_time, GDS_helper_make_put_message(), GDS_helper_put_message_get_size(), GDS_my_identity, GDS_my_identity_hash, GDS_my_private_key, GDS_stats, get_target_peers(), GNUNET_ALIGN, GNUNET_assert, GNUNET_CONTAINER_bloomfilter_add(), GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_DHT_RO_TRUNCATED, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_h2s(), GNUNET_i2s(), GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), PeerPutMessage::header, hello_check(), PeerPutMessage::hop_count, PeerInfo::id, GNUNET_DATACACHE_Block::key, PeerInfo::phash, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, PeerPutMessage::put_path_length, ret, GNUNET_DATACACHE_Block::ro, GNUNET_DATACACHE_Block::trunc_peer, and GNUNET_DATACACHE_Block::type.

Referenced by handle_dht_local_put(), and handle_dht_p2p_put().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_NEIGHBOURS_handle_get()

enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_get ( enum GNUNET_BLOCK_Type  type,
enum GNUNET_DHT_RouteOption  options,
uint16_t  desired_replication_level,
uint16_t  hop_count,
const struct GNUNET_HashCode key,
const void *  xquery,
size_t  xquery_size,
struct GNUNET_BLOCK_Group bg,
struct GNUNET_CONTAINER_BloomFilter peer_bf 
)

Perform a GET operation.

Forwards the given request to other peers. Does not lookup the key locally. May do nothing if this is the only peer in the network (or if we are the closest peer in the network).

Parameters
typetype of the block
optionsrouting options
desired_replication_leveldesired replication count
hop_counthow many hops did this request traverse so far?
keykey for the content
xqueryextended query
xquery_sizenumber of bytes in xquery
bgblock group to filter replies
peer_bffilter for peers not to select (again, updated)
Returns
GNUNET_OK if the request was forwarded, GNUNET_NO if not

Definition at line 1331 of file gnunet-service-dht_neighbours.c.

1340{
1341 unsigned int target_count;
1342 struct PeerInfo **targets;
1343 size_t msize;
1344 size_t result_filter_size;
1345 void *result_filter;
1346
1347 GNUNET_assert (NULL != peer_bf);
1349 "# GET requests routed",
1350 1,
1351 GNUNET_NO);
1352 target_count = get_target_peers (key,
1353 peer_bf,
1354 hop_count,
1355 desired_replication_level,
1356 &targets);
1358 "Adding myself (%s) to GET bloomfilter for %s with RO(%s/%s)\n",
1360 GNUNET_h2s (key),
1362 (options & GNUNET_DHT_RO_RECORD_ROUTE) ? "R" : "-");
1363
1366 if (0 == target_count)
1367 {
1369 "Routing GET for %s terminates after %u hops at %s\n",
1370 GNUNET_h2s (key),
1371 (unsigned int) hop_count,
1373 return GNUNET_NO;
1374 }
1375 if (GNUNET_OK !=
1377 &result_filter,
1378 &result_filter_size))
1379 {
1380 result_filter = NULL;
1381 result_filter_size = 0;
1382 }
1383 msize = xquery_size + result_filter_size;
1384 if (msize + sizeof(struct PeerGetMessage) >= GNUNET_MAX_MESSAGE_SIZE)
1385 {
1386 GNUNET_break (0);
1387 GNUNET_free (result_filter);
1388 GNUNET_free (targets);
1389 return GNUNET_NO;
1390 }
1391 /* update BF */
1392 for (unsigned int i = 0; i < target_count; i++)
1393 {
1394 struct PeerInfo *target = targets[i];
1395
1397 &target->phash);
1398 }
1399 /* forward request */
1400 for (unsigned int i = 0; i < target_count; i++)
1401 {
1402 struct PeerInfo *target = targets[i];
1403 struct PeerGetMessage *pgm;
1404 char buf[sizeof (*pgm) + msize] GNUNET_ALIGN;
1405 char *rf;
1406
1408 "Routing GET for %s after %u hops to %s\n",
1409 GNUNET_h2s (key),
1410 (unsigned int) hop_count,
1411 GNUNET_i2s (&target->id));
1412 pgm = (struct PeerGetMessage *) buf;
1414 pgm->header.size = htons (sizeof (buf));
1415 pgm->type = htonl (type);
1416 pgm->options = htons (options);
1417 pgm->hop_count = htons (hop_count + 1);
1419 pgm->result_filter_size = htons ((uint16_t) result_filter_size);
1422 pgm->bloomfilter,
1424 pgm->key = *key;
1425 rf = (char *) &pgm[1];
1426 GNUNET_memcpy (rf,
1427 result_filter,
1430 xquery,
1431 xquery_size);
1432 do_send (target,
1433 &pgm->header);
1434 }
1436 "# GET messages queued for transmission",
1437 target_count,
1438 GNUNET_NO);
1439 GNUNET_free (targets);
1440 GNUNET_free (result_filter);
1441 return (0 < target_count) ? GNUNET_OK : GNUNET_NO;
1442}
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static uint32_t type
Type string converted to DNS type value.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
enum GNUNET_GenericReturnValue GNUNET_BLOCK_group_serialize(struct GNUNET_BLOCK_Group *bg, void **raw_data, size_t *raw_data_size)
Serialize state of a block group.
Definition: block.c:177
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_bloomfilter_get_raw_data(const struct GNUNET_CONTAINER_BloomFilter *bf, char *data, size_t size)
Copy the raw data of this Bloom filter into the given data array.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MESSAGE_TYPE_DHT_P2P_GET
Peer tries to find data in DHT.
uint16_t result_filter_size
Size of the result filter.
char bloomfilter[128]
Bloomfilter (for peer identities) to stop circular routes.
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_DHT_P2P_GET.
uint16_t hop_count
Hop count.
uint16_t desired_replication_level
Desired replication level for this request.
uint16_t options
Processing options.
struct GNUNET_HashCode key
The key we are looking for.
uint32_t type
Desired content type.

References PeerGetMessage::bloomfilter, PeerGetMessage::desired_replication_level, DHT_BLOOM_SIZE, do_send(), GDS_my_identity, GDS_my_identity_hash, GDS_stats, get_target_peers(), GNUNET_ALIGN, GNUNET_assert, GNUNET_BLOCK_group_serialize(), GNUNET_break, GNUNET_CONTAINER_bloomfilter_add(), GNUNET_CONTAINER_bloomfilter_get_raw_data(), GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_h2s(), GNUNET_i2s(), GNUNET_log, GNUNET_MAX_MESSAGE_SIZE, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), PeerGetMessage::header, PeerGetMessage::hop_count, PeerInfo::id, key, PeerGetMessage::key, options, PeerGetMessage::options, PeerInfo::phash, PeerGetMessage::result_filter_size, GNUNET_MessageHeader::size, type, GNUNET_MessageHeader::type, and PeerGetMessage::type.

Referenced by handle_dht_p2p_get(), send_find_peer_message(), and transmit_request().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_NEIGHBOURS_lookup_peer()

struct PeerInfo * GDS_NEIGHBOURS_lookup_peer ( const struct GNUNET_PeerIdentity target)

Lookup peer by peer's identity.

Parameters
targetpeer to look up
Returns
NULL if we are not connected to target

Definition at line 1446 of file gnunet-service-dht_neighbours.c.

1447{
1449 target);
1450}

References all_connected_peers, and GNUNET_CONTAINER_multipeermap_get().

Referenced by process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_NEIGHBOURS_handle_reply()

bool GDS_NEIGHBOURS_handle_reply ( struct PeerInfo pi,
const struct GNUNET_DATACACHE_Block 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).

Only forwards the reply back to other peers waiting for it. Does not do local caching or forwarding to local clients.

Parameters
pineighbour that should receive the block
bddetails about the reply
query_hashquery that was used for the request
get_path_lengthnumber of entries in put_path
get_pathpeers this reply has traversed so far (if tracked)
Returns
true on success

Definition at line 1454 of file gnunet-service-dht_neighbours.c.

1459{
1460 struct GNUNET_DHT_PathElement *paths;
1461 size_t msize;
1462 unsigned int ppl = bd->put_path_length;
1463 const struct GNUNET_DHT_PathElement *put_path = bd->put_path;
1464 enum GNUNET_DHT_RouteOption ro = bd->ro;
1465 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
1466 const struct GNUNET_PeerIdentity *trunc_peer
1467 = truncated
1468 ? &bd->trunc_peer
1469 : NULL;
1470 bool tracking = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
1471#if SANITY_CHECKS > 1
1472 unsigned int failure_offset;
1473
1474 failure_offset
1476 bd->data_size,
1477 bd->expiration_time,
1478 trunc_peer,
1479 put_path,
1480 ppl,
1481 get_path,
1482 get_path_length,
1484 if (0 != failure_offset)
1485 {
1486 GNUNET_assert (failure_offset <= ppl + get_path_length);
1487 GNUNET_break_op (0);
1488 if (failure_offset < ppl)
1489 {
1490 trunc_peer = &put_path[failure_offset - 1].pred;
1491 put_path += failure_offset;
1492 ppl -= failure_offset;
1493 truncated = true;
1495 }
1496 else
1497 {
1498 failure_offset -= ppl;
1499 if (0 == failure_offset)
1500 trunc_peer = &put_path[ppl - 1].pred;
1501 else
1502 trunc_peer = &get_path[failure_offset - 1].pred;
1503 ppl = 0;
1504 put_path = NULL;
1505 truncated = true;
1507 get_path += failure_offset;
1508 get_path_length -= failure_offset;
1509 }
1510 }
1511#endif
1512 msize = bd->data_size + sizeof (struct PeerResultMessage);
1513 if (msize > GNUNET_MAX_MESSAGE_SIZE)
1514 {
1515 GNUNET_break_op (0);
1516 return false;
1517 }
1518 if (truncated)
1519 msize += sizeof (struct GNUNET_PeerIdentity);
1520 if (tracking)
1521 msize += sizeof (struct GNUNET_CRYPTO_EddsaSignature);
1522 if (msize < bd->data_size)
1523 {
1524 GNUNET_break_op (0);
1525 return false;
1526 }
1527 if ( (GNUNET_MAX_MESSAGE_SIZE - msize)
1528 / sizeof(struct GNUNET_DHT_PathElement)
1529 < (get_path_length + ppl) )
1530 {
1531 get_path_length = 0;
1532 ppl = 0;
1533 }
1534 if ( (get_path_length > UINT16_MAX) ||
1535 (ppl > UINT16_MAX) )
1536 {
1537 GNUNET_break (0);
1538 get_path_length = 0;
1539 ppl = 0;
1540 }
1541 msize += (get_path_length + ppl)
1542 * sizeof(struct GNUNET_DHT_PathElement);
1544 "Forwarding reply for key %s to peer %s\n",
1545 GNUNET_h2s (query_hash),
1546 GNUNET_i2s (&pi->id));
1548 "# RESULT messages queued for transmission",
1549 1,
1550 GNUNET_NO);
1551 {
1552 struct PeerResultMessage *prm;
1553 char buf[msize] GNUNET_ALIGN;
1554 void *data;
1555
1556 prm = (struct PeerResultMessage *) buf;
1558 prm->header.size = htons (sizeof (buf));
1559 prm->type = htonl ((uint32_t) bd->type);
1560 prm->reserved = htons (0);
1561 prm->options = htons ((uint16_t) ro);
1562 prm->put_path_length = htons ((uint16_t) ppl);
1563 prm->get_path_length = htons ((uint16_t) get_path_length);
1565 prm->key = *query_hash;
1566 if (truncated)
1567 {
1568 void *tgt = &prm[1];
1569
1570 GNUNET_memcpy (tgt,
1571 trunc_peer,
1572 sizeof (struct GNUNET_PeerIdentity));
1573 paths = (struct GNUNET_DHT_PathElement *)
1574 (tgt + sizeof (struct GNUNET_PeerIdentity));
1575 }
1576 else
1577 {
1578 paths = (struct GNUNET_DHT_PathElement *) &prm[1];
1579 }
1580 if (NULL != put_path)
1581 {
1582 GNUNET_memcpy (paths,
1583 put_path,
1584 ppl * sizeof(struct GNUNET_DHT_PathElement));
1585 }
1586 else
1587 {
1588 GNUNET_assert (0 == ppl);
1589 }
1590 if (NULL != get_path)
1591 {
1592 GNUNET_memcpy (&paths[ppl],
1593 get_path,
1594 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
1595 }
1596 else
1597 {
1598 GNUNET_assert (0 == get_path_length);
1599 }
1600 if (tracking)
1601 {
1603 void *tgt = &paths[get_path_length + ppl];
1604 const struct GNUNET_PeerIdentity *pred;
1605
1606 if (ppl + get_path_length > 0)
1607 pred = &paths[ppl + get_path_length - 1].pred;
1608 else if (truncated)
1609 pred = trunc_peer;
1610 else
1611 pred = NULL; /* we are first! */
1612 /* Note that the last signature in 'paths' was not initialized before,
1613 so this is crucial to avoid sending garbage. */
1615 bd->data_size,
1617 bd->expiration_time,
1618 pred,
1619 &pi->id,
1620 &sig);
1621 memcpy (tgt,
1622 &sig,
1623 sizeof (sig));
1624 data = tgt + sizeof (sig);
1626 "Signing GET PATH %u/%u of %s => %s\n",
1627 ppl,
1628 get_path_length,
1629 GNUNET_h2s (query_hash),
1630 GNUNET_B2S (&sig));
1631#if SANITY_CHECKS > 1
1632 {
1633 struct GNUNET_DHT_PathElement xpaths[get_path_length + 1];
1634
1635 memcpy (xpaths,
1636 &paths[ppl],
1637 get_path_length * sizeof (struct GNUNET_DHT_PathElement));
1638 xpaths[get_path_length].sig = sig;
1639 xpaths[get_path_length].pred = GDS_my_identity;
1640 if (0 !=
1642 bd->data_size,
1643 bd->expiration_time,
1644 trunc_peer,
1645 paths,
1646 ppl,
1647 xpaths,
1648 get_path_length + 1,
1649 &pi->id))
1650 {
1651 GNUNET_break (0);
1652 return false;
1653 }
1654 }
1655#endif
1656 }
1657 else
1658 {
1659 data = &prm[1];
1660 }
1662 bd->data,
1663 bd->data_size);
1664 do_send (pi,
1665 &prm->header);
1666 }
1667 return true;
1668}
void GDS_helper_sign_path(const void *data, size_t data_size, const struct GNUNET_CRYPTO_EddsaPrivateKey *sk, struct GNUNET_TIME_Absolute exp_time, const struct GNUNET_PeerIdentity *pred, const struct GNUNET_PeerIdentity *succ, struct GNUNET_CRYPTO_EddsaSignature *sig)
Sign that we are routing a message from pred to succ.
Definition: dht_helper.c:151
static char * data
The data to insert into the dht.
static size_t data_size
Number of bytes in data.
unsigned int GNUNET_DHT_verify_path(const void *data, size_t data_size, struct GNUNET_TIME_Absolute exp_time, const struct GNUNET_PeerIdentity *trunc_peer, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_len, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_len, const struct GNUNET_PeerIdentity *me)
Verify signatures on a path consisting of put_path and get_path in reverse order (starting at the las...
Definition: dht_api.c:1349
#define GNUNET_B2S(obj)
Convert a fixed-sized object to a string using GNUNET_b2s().
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
Data is returned to peer from DHT.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:640
an ECC signature using EdDSA.
struct GNUNET_PeerIdentity pred
Previous peer on the path (matches "pred" in the signed field).
struct GNUNET_CRYPTO_EddsaSignature sig
Signature affirming the hop of type GNUNET_SIGNATURE_PURPOSE_DHT_HOP.
uint16_t get_path_length
Length of the GET path that follows (if tracked).
uint16_t options
Message options, actually an 'enum GNUNET_DHT_RouteOption' value in NBO.
struct GNUNET_HashCode key
The key of the corresponding GET request.
struct GNUNET_MessageHeader header
Type: GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT.
uint16_t put_path_length
Length of the PUT path that follows (if tracked).
struct GNUNET_TIME_AbsoluteNBO expiration_time
When does the content expire?

References data, GNUNET_DATACACHE_Block::data, data_size, GNUNET_DATACACHE_Block::data_size, do_send(), GNUNET_DATACACHE_Block::expiration_time, PeerResultMessage::expiration_time, GDS_helper_sign_path(), GDS_my_identity, GDS_my_private_key, GDS_stats, PeerResultMessage::get_path_length, GNUNET_ALIGN, GNUNET_assert, GNUNET_B2S, GNUNET_break, GNUNET_break_op, GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_DHT_RO_TRUNCATED, GNUNET_DHT_verify_path(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s(), GNUNET_i2s(), GNUNET_log, GNUNET_MAX_MESSAGE_SIZE, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, GNUNET_NO, GNUNET_STATISTICS_update(), GNUNET_TIME_absolute_hton(), PeerResultMessage::header, PeerInfo::id, PeerResultMessage::key, PeerResultMessage::options, GNUNET_DHT_PathElement::pred, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, PeerResultMessage::put_path_length, PeerResultMessage::reserved, GNUNET_DATACACHE_Block::ro, GNUNET_DHT_PathElement::sig, GNUNET_MessageHeader::size, GNUNET_DATACACHE_Block::trunc_peer, GNUNET_MessageHeader::type, GNUNET_DATACACHE_Block::type, and PeerResultMessage::type.

Referenced by handle_find_local_hello(), handle_find_my_hello(), handle_local_result(), and process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_dht_p2p_put()

static enum GNUNET_GenericReturnValue check_dht_p2p_put ( void *  cls,
const struct PeerPutMessage put 
)
static

Check validity of a p2p put request.

Parameters
clsclosure with the struct PeerInfo of the sender
putmessage
Returns
GNUNET_OK if the message is valid

Definition at line 1679 of file gnunet-service-dht_neighbours.c.

1681{
1682 enum GNUNET_DHT_RouteOption ro = ntohs (put->options);
1683 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
1684 bool has_path = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
1685 uint16_t msize = ntohs (put->header.size);
1686 uint16_t putlen = ntohs (put->put_path_length);
1687 size_t xsize = (has_path
1688 ? sizeof (struct GNUNET_CRYPTO_EddsaSignature)
1689 : 0)
1690 + (truncated
1691 ? sizeof (struct GNUNET_PeerIdentity)
1692 : 0);
1693 size_t var_meta_size
1694 = putlen * sizeof(struct GNUNET_DHT_PathElement)
1695 + xsize;
1696
1697 (void) cls;
1698 if ( (msize <
1699 sizeof (struct PeerPutMessage) + var_meta_size) ||
1700 (putlen >
1701 (GNUNET_MAX_MESSAGE_SIZE
1702 - sizeof (struct PeerPutMessage)
1703 - xsize)
1704 / sizeof(struct GNUNET_DHT_PathElement)) )
1705 {
1706 GNUNET_break_op (0);
1707 return GNUNET_SYSERR;
1708 }
1709 if (GNUNET_BLOCK_TYPE_ANY == htonl (put->type))
1710 {
1711 GNUNET_break_op (0);
1712 return GNUNET_SYSERR;
1713 }
1714 return GNUNET_OK;
1715}
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
@ GNUNET_SYSERR
uint32_t type
Content type, must not be zero.
Definition: dht.h:438
uint16_t options
Processing options.
Definition: dht.h:443

References GNUNET_BLOCK_TYPE_ANY, GNUNET_break_op, GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_DHT_RO_TRUNCATED, GNUNET_OK, GNUNET_SYSERR, PeerPutMessage::header, PeerPutMessage::options, PeerPutMessage::put_path_length, GNUNET_MessageHeader::size, and PeerPutMessage::type.

◆ handle_dht_p2p_put()

static void handle_dht_p2p_put ( void *  cls,
const struct PeerPutMessage put 
)
static

Core handler for p2p put requests.

Parameters
clsclosure with the struct Target of the sender
putmessage

Definition at line 1725 of file gnunet-service-dht_neighbours.c.

1727{
1728 struct Target *t = cls;
1729 struct PeerInfo *peer = t->pi;
1730 enum GNUNET_DHT_RouteOption ro = ntohs (put->options);
1731 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
1732 bool has_path = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
1733 uint16_t msize = ntohs (put->header.size);
1734 uint16_t putlen = ntohs (put->put_path_length);
1735 const struct GNUNET_PeerIdentity *trunc_peer
1736 = truncated
1737 ? (const struct GNUNET_PeerIdentity *) &put[1]
1738 : NULL;
1739 const struct GNUNET_DHT_PathElement *put_path
1740 = truncated
1741 ? (const struct GNUNET_DHT_PathElement *) &trunc_peer[1]
1742 : (const struct GNUNET_DHT_PathElement *) &put[1];
1743 const struct GNUNET_CRYPTO_EddsaSignature *last_sig
1744 = has_path
1745 ? (const struct GNUNET_CRYPTO_EddsaSignature *) &put_path[putlen]
1746 : NULL;
1747 const char *data
1748 = has_path
1749 ? (const char *) &last_sig[1]
1750 : (const char *) &put_path[putlen];
1751 size_t var_meta_size
1752 = putlen * sizeof(struct GNUNET_DHT_PathElement)
1753 + (has_path ? sizeof (*last_sig) : 0)
1754 + (truncated ? sizeof (*trunc_peer) : 0);
1755 struct GNUNET_DATACACHE_Block bd = {
1756 .key = put->key,
1757 .expiration_time = GNUNET_TIME_absolute_ntoh (put->expiration_time),
1758 .type = ntohl (put->type),
1759 .ro = ro,
1760 .data_size = msize - sizeof(*put) - var_meta_size,
1761 .data = data
1762 };
1763
1764 if (NULL != trunc_peer)
1765 bd.trunc_peer = *trunc_peer;
1767 "PUT for `%s' from %s with RO (%s/%s)\n",
1768 GNUNET_h2s (&put->key),
1769 GNUNET_i2s (&peer->id),
1770 (bd.ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE) ? "x" : "-",
1771 has_path ? "R" : "-");
1773 {
1775 "# Expired PUTs discarded",
1776 1,
1777 GNUNET_NO);
1778 return;
1779 }
1780 {
1781 /* Only call 'check_block' if that keeps our CPU load (from
1782 the cryptography) below 50% on average */
1783 static struct GNUNET_TIME_Relative avg_latency;
1784 static struct GNUNET_TIME_Absolute next_time;
1785
1786 if (GNUNET_TIME_absolute_is_past (next_time))
1787 {
1788 struct GNUNET_TIME_Absolute now
1790 struct GNUNET_TIME_Relative latency;
1792
1793 if (GNUNET_NO ==
1795 bd.type,
1796 bd.data,
1797 bd.data_size))
1798 {
1799 GNUNET_break_op (0);
1800 return;
1801 }
1802 latency = GNUNET_TIME_absolute_get_duration (now);
1803 /* Use *moving average* to estimate check_block latency */
1804 avg_latency
1807 GNUNET_TIME_relative_multiply (avg_latency,
1808 7),
1809 latency),
1810 8);
1811 /* average delay = 50% of avg_latency => 50% CPU load from crypto (at most) */
1814 avg_latency.rel_value_us > 0
1815 ? avg_latency.rel_value_us
1816 : 1LLU);
1818 }
1819 }
1820 if (! has_path)
1821 putlen = 0;
1823 "# P2P PUT requests received",
1824 1,
1825 GNUNET_NO);
1827 "# P2P PUT bytes received",
1828 msize,
1829 GNUNET_NO);
1830 {
1831 struct GNUNET_HashCode test_key;
1833
1835 bd.type,
1836 bd.data,
1837 bd.data_size,
1838 &test_key);
1839 switch (ret)
1840 {
1841 case GNUNET_YES:
1842 if (0 != GNUNET_memcmp (&test_key,
1843 &bd.key))
1844 {
1845 GNUNET_break_op (0);
1846 return;
1847 }
1848 break;
1849 case GNUNET_NO:
1850 /* cannot verify, good luck */
1851 break;
1852 case GNUNET_SYSERR:
1853 /* block type not supported, good luck */
1854 break;
1855 }
1856 }
1857
1858 {
1860 struct GNUNET_DHT_PathElement pp[putlen + 1];
1861
1867 &peer->phash));
1868 /* extend 'put path' by sender */
1869 bd.put_path = pp;
1870 bd.put_path_length = putlen + 1;
1871 if (has_path)
1872 {
1873 unsigned int failure_offset;
1874
1875 GNUNET_memcpy (pp,
1876 put_path,
1877 putlen * sizeof(struct GNUNET_DHT_PathElement));
1878 pp[putlen].pred = peer->id;
1879 pp[putlen].sig = *last_sig;
1880#if SANITY_CHECKS
1881 /* TODO: might want to eventually implement probabilistic
1882 load-based path verification, but for now it is all or nothing */
1883 failure_offset
1885 bd.data_size,
1886 bd.expiration_time,
1887 trunc_peer,
1888 pp,
1889 putlen + 1,
1890 NULL, 0, /* get_path */
1892#else
1893 failure_offset = 0;
1894#endif
1895 if (0 != failure_offset)
1896 {
1897 GNUNET_break_op (0);
1899 "Recorded put path invalid at offset %u, truncating\n",
1900 failure_offset);
1901 GNUNET_assert (failure_offset <= putlen + 1);
1902 bd.put_path = &pp[failure_offset];
1903 bd.put_path_length = (putlen + 1) - failure_offset;
1905 bd.trunc_peer = pp[failure_offset - 1].pred;
1906 }
1907 }
1908 else
1909 {
1910 bd.put_path_length = 0;
1911 }
1912
1913 /* give to local clients */
1915 &bd.key,
1916 0, NULL /* get path */));
1917
1918 /* store locally */
1919 if ( (0 != (bd.ro & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1920 (GDS_am_closest_peer (&put->key,
1921 bf)) )
1923 {
1924 enum GNUNET_GenericReturnValue forwarded;
1925
1926 /* route to other peers */
1927 forwarded
1929 ntohs (put->desired_replication_level),
1930 ntohs (put->hop_count),
1931 bf);
1932 /* notify monitoring clients */
1933 bd.ro |= ((GNUNET_OK == forwarded)
1935 : 0);
1937 ntohs (put->hop_count),
1938 ntohs (put->desired_replication_level));
1939 }
1941 }
1942}
void GDS_CLIENTS_process_put(const struct GNUNET_DATACACHE_Block *bd, uint32_t hop_count, uint32_t desired_replication_level)
Check if some client is monitoring PUT messages and notify them in that case.
bool GDS_CLIENTS_handle_reply(const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path)
Handle a reply we've received from another peer.
void GDS_DATACACHE_handle_put(const struct GNUNET_DATACACHE_Block *bd)
Handle a datum we've received from another peer.
enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_put(const struct GNUNET_DATACACHE_Block *bd, uint16_t desired_replication_level, uint16_t hop_count, struct GNUNET_CONTAINER_BloomFilter *bf)
Perform a PUT operation.
enum GNUNET_GenericReturnValue GDS_am_closest_peer(const struct GNUNET_HashCode *key, const struct GNUNET_CONTAINER_BloomFilter *bloom)
Check whether my identity is closer than any known peers.
enum GNUNET_GenericReturnValue GNUNET_BLOCK_check_block(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *block, size_t block_size)
Function called to validate a block.
Definition: block.c:321
enum GNUNET_GenericReturnValue GNUNET_BLOCK_get_key(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const void *block, size_t block_size, struct GNUNET_HashCode *key)
Function called to obtain the key for a block.
Definition: block.c:278
@ GNUNET_DHT_RO_LAST_HOP
Flag given to monitors if this was the last hop for a GET/PUT.
@ GNUNET_ERROR_TYPE_WARNING
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:438
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:741
struct GNUNET_TIME_Relative GNUNET_TIME_relative_add(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Add relative times together.
Definition: time.c:587
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:316
struct GNUNET_TIME_Relative GNUNET_TIME_relative_divide(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Divide relative time by a given factor.
Definition: time.c:552
bool GNUNET_TIME_absolute_is_past(struct GNUNET_TIME_Absolute abs)
Test if abs is truly in the past (excluding now).
Definition: time.c:671
Information about a block stored in the datacache.
Time for absolute times used by GNUnet, in microseconds.
char bloomfilter[128]
Bloomfilter (for peer identities) to stop circular routes.
Definition: dht.h:468
struct GNUNET_HashCode key
The key we are storing under.
Definition: dht.h:473
struct GNUNET_TIME_AbsoluteNBO expiration_time
When does the content expire?
Definition: dht.h:463

References PeerPutMessage::bloomfilter, data, GNUNET_DATACACHE_Block::data, GNUNET_DATACACHE_Block::data_size, delta, PeerPutMessage::desired_replication_level, DHT_BLOOM_SIZE, GNUNET_DATACACHE_Block::expiration_time, PeerPutMessage::expiration_time, GDS_am_closest_peer(), GDS_block_context, GDS_CLIENTS_handle_reply(), GDS_CLIENTS_process_put(), GDS_DATACACHE_handle_put(), GDS_my_identity, GDS_NEIGHBOURS_handle_put(), GDS_stats, GNUNET_assert, GNUNET_BLOCK_check_block(), GNUNET_BLOCK_get_key(), GNUNET_break, GNUNET_break_op, GNUNET_CONSTANTS_BLOOMFILTER_K, GNUNET_CONTAINER_bloomfilter_free(), GNUNET_CONTAINER_bloomfilter_init(), GNUNET_CONTAINER_bloomfilter_test(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_DHT_RO_LAST_HOP, GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_DHT_RO_TRUNCATED, GNUNET_DHT_verify_path(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_h2s(), GNUNET_i2s(), GNUNET_log, GNUNET_memcmp, GNUNET_memcpy, GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_get_duration(), GNUNET_TIME_absolute_is_past(), GNUNET_TIME_absolute_ntoh(), GNUNET_TIME_relative_add(), GNUNET_TIME_relative_divide(), GNUNET_TIME_relative_multiply(), GNUNET_TIME_relative_to_absolute(), GNUNET_YES, PeerPutMessage::header, PeerPutMessage::hop_count, PeerInfo::id, GNUNET_DATACACHE_Block::key, PeerPutMessage::key, PeerPutMessage::options, PeerInfo::phash, GNUNET_DHT_PathElement::pred, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, PeerPutMessage::put_path_length, GNUNET_TIME_Relative::rel_value_us, ret, GNUNET_DATACACHE_Block::ro, GNUNET_DHT_PathElement::sig, GNUNET_MessageHeader::size, t, GNUNET_DATACACHE_Block::trunc_peer, GNUNET_DATACACHE_Block::type, and PeerPutMessage::type.

Here is the call graph for this function:

◆ handle_find_my_hello()

static void handle_find_my_hello ( struct PeerInfo pi,
const struct GNUNET_HashCode query_hash,
struct GNUNET_BLOCK_Group bg 
)
static

We have received a request for a HELLO.

Sends our HELLO back.

Parameters
pisender of the request
keypeers close to this key are desired
bggroup for filtering peers

Definition at line 1954 of file gnunet-service-dht_neighbours.c.

1957{
1958 size_t block_size = 0;
1959
1960 /* TODO: consider caching our HELLO block for a bit, to
1961 avoid signing too often here... */
1965 NULL,
1966 &block_size,
1968 {
1969 char block[block_size];
1970
1971 if (GNUNET_OK !=
1974 block,
1975 &block_size,
1977 {
1979 "# FIND PEER requests ignored due to lack of HELLO",
1980 1,
1981 GNUNET_NO);
1982 }
1983 else if (GNUNET_BLOCK_REPLY_OK_MORE ==
1986 bg,
1988 NULL, 0,
1989 block,
1990 block_size))
1991 {
1992 struct GNUNET_DATACACHE_Block bd = {
1994 .expiration_time
1997 .key = GDS_my_identity_hash,
1998 .data = block,
1999 .data_size = block_size
2000 };
2001
2003 &bd,
2004 query_hash,
2005 0, NULL /* get path */));
2006 }
2007 else
2008 {
2010 "# FIND PEER requests ignored due to Bloomfilter",
2011 1,
2012 GNUNET_NO);
2013 }
2014 }
2015}
struct GNUNET_HELLO_Builder * GDS_my_hello
Our HELLO.
bool GDS_NEIGHBOURS_handle_reply(struct PeerInfo *pi, const struct GNUNET_DATACACHE_Block *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).
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 if a reply is good for a particular query.
Definition: block.c:339
@ GNUNET_BLOCK_REPLY_OK_MORE
Valid result, and there may be more.
enum GNUNET_GenericReturnValue GNUNET_HELLO_builder_to_block(const struct GNUNET_HELLO_Builder *builder, const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, void *block, size_t *block_size, struct GNUNET_TIME_Relative expiration_time)
Generate DHT block from a builder.
Definition: hello-uri.c:1006
#define GNUNET_HELLO_ADDRESS_EXPIRATION
For how long are HELLO signatures valid?
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.

References GDS_block_context, GDS_my_hello, GDS_my_identity_hash, GDS_my_private_key, GDS_NEIGHBOURS_handle_reply(), GDS_stats, GNUNET_BLOCK_check_reply(), GNUNET_BLOCK_REPLY_OK_MORE, GNUNET_BLOCK_TYPE_DHT_HELLO, GNUNET_break, GNUNET_HELLO_ADDRESS_EXPIRATION, GNUNET_HELLO_builder_to_block(), GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), GNUNET_TIME_relative_to_absolute(), GNUNET_TIME_UNIT_ZERO, and GNUNET_DATACACHE_Block::type.

Referenced by handle_dht_p2p_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_find_local_hello()

static void handle_find_local_hello ( struct PeerInfo pi,
const struct GNUNET_HashCode query_hash,
struct GNUNET_BLOCK_Group bg 
)
static

We have received a request for nearby HELLOs.

Sends matching HELLOs back.

Parameters
pisender of the request
keypeers close to this key are desired
bggroup for filtering peers

Definition at line 2027 of file gnunet-service-dht_neighbours.c.

2030{
2031 /* Force non-random selection by hop count */
2032 struct PeerInfo *peer;
2033
2034 peer = select_peer (query_hash,
2035 NULL,
2036 GDS_NSE_get () + 1);
2037 if ( (NULL != peer->hello) &&
2043 bg,
2044 &peer->phash,
2045 NULL, 0, /* xquery */
2046 peer->hello,
2047 peer->hello_size)) )
2048 {
2049 struct GNUNET_DATACACHE_Block bd = {
2051 .expiration_time = peer->hello_expiration,
2052 .key = peer->phash,
2053 .data = peer->hello,
2054 .data_size = peer->hello_size
2055 };
2056
2058 &bd,
2059 query_hash,
2060 0, NULL /* get path */));
2061 }
2062}
struct GNUNET_TIME_Absolute hello_expiration
When does our HELLO from this peer expire?
size_t hello_size
Number of bytes in hello.

References GDS_block_context, GDS_NEIGHBOURS_handle_reply(), GDS_NSE_get(), GNUNET_BLOCK_check_reply(), GNUNET_BLOCK_REPLY_OK_MORE, GNUNET_BLOCK_TYPE_DHT_HELLO, GNUNET_break, GNUNET_TIME_absolute_is_past(), PeerInfo::hello, PeerInfo::hello_expiration, PeerInfo::hello_size, PeerInfo::phash, select_peer(), and GNUNET_DATACACHE_Block::type.

Referenced by handle_dht_p2p_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_local_result()

static void handle_local_result ( void *  cls,
const struct GNUNET_DATACACHE_Block bd 
)
static

Handle an exact result from local datacache for a GET operation.

Parameters
clsthe struct PeerInfo for which this is a reply
bddetails about the block we found locally

Definition at line 2072 of file gnunet-service-dht_neighbours.c.

2074{
2075 struct PeerInfo *peer = cls;
2076
2078 bd,
2079 &bd->key,
2080 0, NULL /* get path */));
2081}

References GDS_NEIGHBOURS_handle_reply(), GNUNET_break, and GNUNET_DATACACHE_Block::key.

Referenced by handle_dht_p2p_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_dht_p2p_get()

static enum GNUNET_GenericReturnValue check_dht_p2p_get ( void *  cls,
const struct PeerGetMessage get 
)
static

Check validity of p2p get request.

Parameters
clsclosure with the struct Target of the sender
getthe message
Returns
GNUNET_OK if the message is well-formed

Definition at line 2092 of file gnunet-service-dht_neighbours.c.

2094{
2095 uint16_t msize = ntohs (get->header.size);
2096 uint16_t result_filter_size = ntohs (get->result_filter_size);
2097
2098 (void) cls;
2099 if (msize < sizeof(*get) + result_filter_size)
2100 {
2101 GNUNET_break_op (0);
2102 return GNUNET_SYSERR;
2103 }
2104 return GNUNET_OK;
2105}
static int get
Get DID Documement for DID Flag.
Definition: gnunet-did.c:63

References get, GNUNET_break_op, GNUNET_OK, and GNUNET_SYSERR.

◆ handle_dht_p2p_get()

static void handle_dht_p2p_get ( void *  cls,
const struct PeerGetMessage get 
)
static

Core handler for p2p get requests.

Parameters
clsclosure with the struct Target of the sender
getthe message

Definition at line 2115 of file gnunet-service-dht_neighbours.c.

2117{
2118 struct Target *t = cls;
2119 struct PeerInfo *peer = t->pi;
2120 uint16_t msize = ntohs (get->header.size);
2121 uint16_t result_filter_size = ntohs (get->result_filter_size);
2122 uint16_t hop_count = ntohs (get->hop_count);
2123 enum GNUNET_BLOCK_Type type = ntohl (get->type);
2124 enum GNUNET_DHT_RouteOption options = ntohs (get->options);
2126 const void *result_filter = (const void *) &get[1];
2127 const void *xquery = result_filter + result_filter_size;
2128 size_t xquery_size = msize - sizeof (*get) - result_filter_size;
2129
2130 /* parse and validate message */
2132 "# P2P GET requests received",
2133 1,
2134 GNUNET_NO);
2136 "# P2P GET bytes received",
2137 msize,
2138 GNUNET_NO);
2139 if (GNUNET_NO ==
2141 type,
2142 &get->key,
2143 xquery,
2144 xquery_size))
2145 {
2146 /* request invalid */
2147 GNUNET_break_op (0);
2148 return;
2149 }
2150
2151 {
2152 struct GNUNET_BLOCK_Group *bg;
2153 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
2154
2155 peer_bf = GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
2158 ;
2161 &peer->phash));
2163 type,
2164 result_filter,
2165 result_filter_size,
2166 "filter-size",
2167 result_filter_size,
2168 NULL);
2170 "GET for %s at %s after %u hops\n",
2171 GNUNET_h2s (&get->key),
2173 (unsigned int) hop_count);
2174 /* local lookup (this may update the bg) */
2176 (GDS_am_closest_peer (&get->key,
2177 peer_bf)) )
2178 {
2181 {
2183 "# P2P HELLO lookup requests processed",
2184 1,
2185 GNUNET_NO);
2187 &get->key,
2188 bg);
2191 &get->key,
2192 bg);
2193 }
2195 {
2197 eval = GDS_DATACACHE_get_closest (&get->key,
2198 type,
2199 xquery,
2200 xquery_size,
2201 bg,
2203 peer);
2204 else
2205 eval = GDS_DATACACHE_handle_get (&get->key,
2206 type,
2207 xquery,
2208 xquery_size,
2209 bg,
2211 peer);
2212 }
2213 }
2214 else
2215 {
2217 "# P2P GET requests ONLY routed",
2218 1,
2219 GNUNET_NO);
2220 }
2221
2222 /* remember request for routing replies
2223 TODO: why should we do this if GNUNET_BLOCK_REPLY_OK_LAST == eval?
2224 */
2225 GDS_ROUTING_add (&peer->id,
2226 type,
2227 bg, /* bg now owned by routing, but valid at least until end of this function! */
2228 options,
2229 &get->key,
2230 xquery,
2231 xquery_size);
2232
2233 /* P2P forwarding */
2234 {
2235 bool forwarded = false;
2236 uint16_t desired_replication_level = ntohs (
2237 get->desired_replication_level);
2238
2239 if (eval != GNUNET_BLOCK_REPLY_OK_LAST)
2240 forwarded = (GNUNET_OK ==
2242 options,
2243 desired_replication_level,
2244 hop_count,
2245 &get->key,
2246 xquery,
2247 xquery_size,
2248 bg,
2249 peer_bf));
2251 options
2252 | (forwarded
2253 ? 0
2255 type,
2256 hop_count,
2257 desired_replication_level,
2258 &get->key);
2259 }
2260 /* clean up; note that 'bg' is owned by routing now! */
2262 }
2263}
void GDS_CLIENTS_process_get(enum GNUNET_DHT_RouteOption options, enum GNUNET_BLOCK_Type type, uint32_t hop_count, uint32_t desired_replication_level, const struct GNUNET_HashCode *key)
Check if some client is monitoring GET messages and notify them in that case.
enum GNUNET_BLOCK_ReplyEvaluationResult GDS_DATACACHE_get_closest(const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, const void *xquery, size_t xquery_size, struct GNUNET_BLOCK_Group *bg, GDS_DATACACHE_GetCallback cb, void *cb_cls)
Handle a request for data close to a key that we have received from another peer.
enum GNUNET_BLOCK_ReplyEvaluationResult GDS_DATACACHE_handle_get(const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, const void *xquery, size_t xquery_size, struct GNUNET_BLOCK_Group *bg, GDS_DATACACHE_GetCallback gc, void *gc_cls)
Handle a GET request we've received from another peer.
static void handle_find_local_hello(struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg)
We have received a request for nearby HELLOs.
static void handle_local_result(void *cls, const struct GNUNET_DATACACHE_Block *bd)
Handle an exact result from local datacache for a GET operation.
static void handle_find_my_hello(struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg)
We have received a request for a HELLO.
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.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
enum GNUNET_GenericReturnValue GNUNET_BLOCK_check_query(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode *query, const void *xquery, size_t xquery_size)
Function called to validate a request.
Definition: block.c:298
GNUNET_BLOCK_ReplyEvaluationResult
Possible ways for how a block may relate to a query.
@ GNUNET_BLOCK_REPLY_OK_LAST
Last possible valid result.

References DHT_BLOOM_SIZE, GDS_am_closest_peer(), GDS_block_context, GDS_CLIENTS_process_get(), GDS_DATACACHE_get_closest(), GDS_DATACACHE_handle_get(), GDS_my_identity, GDS_NEIGHBOURS_handle_get(), GDS_ROUTING_add(), GDS_stats, get, GNUNET_BLOCK_check_query(), GNUNET_BLOCK_group_create(), GNUNET_BLOCK_REPLY_OK_LAST, GNUNET_BLOCK_REPLY_OK_MORE, GNUNET_BLOCK_TYPE_ANY, GNUNET_BLOCK_TYPE_DHT_HELLO, GNUNET_break_op, GNUNET_CONSTANTS_BLOOMFILTER_K, GNUNET_CONTAINER_bloomfilter_free(), GNUNET_CONTAINER_bloomfilter_init(), GNUNET_CONTAINER_bloomfilter_test(), GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_DHT_RO_FIND_APPROXIMATE, GNUNET_DHT_RO_LAST_HOP, GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s(), GNUNET_i2s(), GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), GNUNET_YES, handle_find_local_hello(), handle_find_my_hello(), handle_local_result(), PeerInfo::id, options, PeerInfo::phash, t, and type.

Here is the call graph for this function:

◆ process_reply_with_path()

static bool process_reply_with_path ( const struct GNUNET_DATACACHE_Block bd,
const struct GNUNET_HashCode query_hash,
unsigned int  get_path_length,
const struct GNUNET_DHT_PathElement get_path 
)
static

Process a reply, after the get_path has been updated.

Parameters
bdblock details
query_hashhash of the original query, might not match key in bd
get_path_lengthnumber of entries in get_path
get_pathpath the reply has taken
Returns
true on success

Definition at line 2276 of file gnunet-service-dht_neighbours.c.

2280{
2281 /* forward to local clients */
2283 "Forwarding reply to local clients\n");
2284 if (! GDS_CLIENTS_handle_reply (bd,
2285 query_hash,
2286 get_path_length,
2287 get_path))
2288 {
2289 GNUNET_break (0);
2290 return false;
2291 }
2293 get_path,
2294 get_path_length);
2296 {
2297 struct GNUNET_DHT_PathElement xput_path[GNUNET_NZL (get_path_length
2298 + bd->put_path_length)];
2299 struct GNUNET_DATACACHE_Block bdx = *bd;
2300
2301 if (NULL != bd->put_path)
2302 GNUNET_memcpy (xput_path,
2303 bd->put_path,
2304 bd->put_path_length * sizeof(struct
2306 GNUNET_memcpy (&xput_path[bd->put_path_length],
2307 get_path,
2308 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
2309 bdx.put_path = xput_path;
2310 bdx.put_path_length += get_path_length;
2312 }
2313 /* forward to other peers */
2315 query_hash,
2316 get_path_length,
2317 get_path);
2318 return true;
2319}
void GDS_CLIENTS_process_get_resp(const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_length)
Check if some client is monitoring GET RESP messages and notify them in that case.
static int cache_results
Do we cache all results that we are routing in the local datacache?
void GDS_ROUTING_process(const struct GNUNET_DATACACHE_Block *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).
#define GNUNET_NZL(l)
Macro used to avoid using 0 for the length of a variable-size array (Non-Zero-Length).

References cache_results, GDS_CLIENTS_handle_reply(), GDS_CLIENTS_process_get_resp(), GDS_DATACACHE_handle_put(), GDS_ROUTING_process(), GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_memcpy, GNUNET_NZL, GNUNET_YES, GNUNET_DATACACHE_Block::put_path, and GNUNET_DATACACHE_Block::put_path_length.

Referenced by handle_dht_p2p_result().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_dht_p2p_result()

static enum GNUNET_GenericReturnValue check_dht_p2p_result ( void *  cls,
const struct PeerResultMessage prm 
)
static

Check validity of p2p result message.

Parameters
clsclosure
prmmessage
Returns
GNUNET_YES if the message is well-formed

Definition at line 2330 of file gnunet-service-dht_neighbours.c.

2332{
2333 uint16_t msize = ntohs (prm->header.size) - sizeof (*prm);
2334 enum GNUNET_DHT_RouteOption ro = ntohs (prm->options);
2335 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
2336 bool tracked = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
2337
2338 uint16_t get_path_length = ntohs (prm->get_path_length);
2339 uint16_t put_path_length = ntohs (prm->put_path_length);
2340 size_t vsize = (truncated ? sizeof (struct GNUNET_PeerIdentity) : 0)
2341 + (tracked ? sizeof (struct GNUNET_CRYPTO_EddsaSignature) : 0);
2342
2343 (void) cls;
2344 if ( (msize < vsize) ||
2345 (msize - vsize <
2346 (get_path_length + put_path_length)
2347 * sizeof(struct GNUNET_DHT_PathElement)) ||
2348 (get_path_length >
2349 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_DHT_PathElement)) ||
2350 (put_path_length >
2351 GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_DHT_PathElement)) )
2352 {
2353 GNUNET_break_op (0);
2354 return GNUNET_SYSERR;
2355 }
2356 return GNUNET_OK;
2357}

References PeerResultMessage::get_path_length, GNUNET_break_op, GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_DHT_RO_TRUNCATED, GNUNET_OK, GNUNET_SYSERR, PeerResultMessage::header, PeerResultMessage::options, GNUNET_DATACACHE_Block::put_path_length, PeerResultMessage::put_path_length, GNUNET_DATACACHE_Block::ro, and GNUNET_MessageHeader::size.

◆ handle_dht_p2p_result()

static void handle_dht_p2p_result ( void *  cls,
const struct PeerResultMessage prm 
)
static

Core handler for p2p result messages.

Parameters
clsclosure
prmmessage

Definition at line 2367 of file gnunet-service-dht_neighbours.c.

2369{
2370 struct Target *t = cls;
2371 struct PeerInfo *peer = t->pi;
2372 uint16_t msize = ntohs (prm->header.size) - sizeof (*prm);
2373 enum GNUNET_DHT_RouteOption ro = ntohs (prm->options);
2374 bool truncated = (0 != (ro & GNUNET_DHT_RO_TRUNCATED));
2375 bool tracked = (0 != (ro & GNUNET_DHT_RO_RECORD_ROUTE));
2376 uint16_t get_path_length = ntohs (prm->get_path_length);
2377 uint16_t put_path_length = ntohs (prm->put_path_length);
2378 const struct GNUNET_PeerIdentity *trunc_peer
2379 = truncated
2380 ? (const struct GNUNET_PeerIdentity *) &prm[1]
2381 : NULL;
2382 const struct GNUNET_DHT_PathElement *put_path
2383 = truncated
2384 ? (const struct GNUNET_DHT_PathElement *) &trunc_peer[1]
2385 : (const struct GNUNET_DHT_PathElement *) &prm[1];
2386 const struct GNUNET_DHT_PathElement *get_path
2387 = &put_path[put_path_length];
2388 const struct GNUNET_CRYPTO_EddsaSignature *last_sig
2389 = tracked
2390 ? (const struct GNUNET_CRYPTO_EddsaSignature *) &get_path[get_path_length]
2391 : NULL;
2392 const void *data
2393 = tracked
2394 ? (const void *) &last_sig[1]
2395 : (const void *) &get_path[get_path_length];
2396 size_t vsize = (truncated ? sizeof (struct GNUNET_PeerIdentity) : 0)
2397 + (tracked ? sizeof (struct GNUNET_CRYPTO_EddsaSignature) : 0);
2398 struct GNUNET_DATACACHE_Block bd = {
2400 .put_path = put_path,
2401 .put_path_length = put_path_length,
2402 .key = prm->key,
2403 .type = ntohl (prm->type),
2404 .ro = ro,
2405 .data = data,
2406 .data_size = msize - vsize - (get_path_length + put_path_length)
2407 * sizeof(struct GNUNET_DHT_PathElement)
2408 };
2409
2410 /* parse and validate message */
2411 if (GNUNET_TIME_absolute_is_past (bd.expiration_time))
2412 {
2414 "# Expired results discarded",
2415 1,
2416 GNUNET_NO);
2417 return;
2418 }
2419 if (GNUNET_OK !=
2421 bd.type,
2422 bd.data,
2423 bd.data_size))
2424 {
2425 GNUNET_break_op (0);
2426 return;
2427 }
2429 "# P2P RESULTS received",
2430 1,
2431 GNUNET_NO);
2433 "# P2P RESULT bytes received",
2434 msize,
2435 GNUNET_NO);
2436 {
2438
2440 bd.type,
2441 bd.data,
2442 bd.data_size,
2443 &bd.key);
2444 if (GNUNET_NO == ret)
2445 bd.key = prm->key;
2446 }
2447
2448 /* if we got a HELLO, consider it for our own routing table */
2449 hello_check (&bd);
2450
2451 /* Need to append 'peer' to 'get_path' */
2452 if (tracked)
2453 {
2454 struct GNUNET_DHT_PathElement xget_path[get_path_length + 1];
2455 struct GNUNET_DHT_PathElement *gp = xget_path;
2456 unsigned int failure_offset;
2457
2458 GNUNET_memcpy (xget_path,
2459 get_path,
2460 get_path_length * sizeof(struct GNUNET_DHT_PathElement));
2461 xget_path[get_path_length].pred = peer->id;
2462 /* use memcpy(), as last_sig may not be aligned */
2463 memcpy (&xget_path[get_path_length].sig,
2464 last_sig,
2465 sizeof (*last_sig));
2466#if SANITY_CHECKS
2467 /* TODO: might want to eventually implement probabilistic
2468 load-based path verification, but for now it is all or nothing */
2469 failure_offset
2470 = GNUNET_DHT_verify_path (bd.data,
2471 bd.data_size,
2472 bd.expiration_time,
2473 trunc_peer,
2474 put_path,
2475 put_path_length,
2476 gp,
2477 get_path_length + 1,
2479#else
2480 failure_offset = 0;
2481#endif
2482 if (0 != failure_offset)
2483 {
2485 "Recorded path invalid at offset %u, truncating\n",
2486 failure_offset);
2487 GNUNET_assert (failure_offset <= bd.put_path_length + get_path_length
2488 + 1);
2489 if (failure_offset < bd.put_path_length)
2490 {
2491 /* failure on put path */
2492 trunc_peer = &bd.put_path[failure_offset - 1].pred;
2493 bd.ro |= GNUNET_DHT_RO_TRUNCATED;
2494 bd.put_path = &bd.put_path[failure_offset];
2495 bd.put_path_length -= failure_offset;
2496 truncated = true;
2497 }
2498 else
2499 {
2500 /* failure on get path */
2501 failure_offset -= bd.put_path_length;
2502 if (0 == failure_offset)
2503 trunc_peer = &bd.put_path[bd.put_path_length - 1].pred;
2504 else
2505 trunc_peer = &gp[failure_offset - 1].pred;
2506 get_path_length -= failure_offset;
2507 gp = &gp[failure_offset];
2508 bd.put_path_length = 0;
2509 bd.put_path = NULL;
2510 bd.ro |= GNUNET_DHT_RO_TRUNCATED;
2511 truncated = true;
2512 }
2513 }
2515 "Extending GET path of length %u with %s\n",
2516 get_path_length,
2517 GNUNET_i2s (&peer->id));
2518 if (truncated)
2519 {
2520 GNUNET_assert (NULL != trunc_peer);
2521 bd.trunc_peer = *trunc_peer;
2522 }
2524 &prm->key,
2525 get_path_length + 1,
2526 gp));
2527 }
2528 else
2529 {
2530 if (truncated)
2531 {
2532 GNUNET_assert (NULL != trunc_peer);
2533 bd.trunc_peer = *trunc_peer;
2534 }
2536 &prm->key,
2537 0,
2538 NULL));
2539 }
2540}
static bool process_reply_with_path(const struct GNUNET_DATACACHE_Block *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *get_path)
Process a reply, after the get_path has been updated.

References data, GNUNET_DATACACHE_Block::expiration_time, PeerResultMessage::expiration_time, GDS_block_context, GDS_my_identity, GDS_stats, PeerResultMessage::get_path_length, GNUNET_assert, GNUNET_BLOCK_check_block(), GNUNET_BLOCK_get_key(), GNUNET_break, GNUNET_break_op, GNUNET_DHT_RO_RECORD_ROUTE, GNUNET_DHT_RO_TRUNCATED, GNUNET_DHT_verify_path(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_i2s(), GNUNET_log, GNUNET_memcpy, GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), GNUNET_TIME_absolute_is_past(), GNUNET_TIME_absolute_ntoh(), PeerResultMessage::header, hello_check(), PeerInfo::id, PeerResultMessage::key, PeerResultMessage::options, GNUNET_DHT_PathElement::pred, process_reply_with_path(), PeerResultMessage::put_path_length, ret, GNUNET_DHT_PathElement::sig, GNUNET_MessageHeader::size, t, and PeerResultMessage::type.

Here is the call graph for this function:

◆ check_dht_p2p_hello()

static enum GNUNET_GenericReturnValue check_dht_p2p_hello ( void *  cls,
const struct GNUNET_MessageHeader hello 
)
static

Check validity of a p2p hello message.

Parameters
clsclosure
hellomessage
Returns
GNUNET_YES if the message is well-formed

Definition at line 2551 of file gnunet-service-dht_neighbours.c.

2553{
2554 struct Target *t = cls;
2555 struct PeerInfo *peer = t->pi;
2557 size_t hellob_size;
2558 void *hellob;
2560
2562 &peer->id,
2563 &hellob,
2564 &hellob_size,
2565 &expiration);
2566 GNUNET_free (hellob);
2567 return ret;
2568}
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
enum GNUNET_GenericReturnValue GNUNET_HELLO_dht_msg_to_block(const struct GNUNET_MessageHeader *hello, const struct GNUNET_PeerIdentity *pid, void **block, size_t *block_size, struct GNUNET_TIME_Absolute *block_expiration)
Convert a DHT hello message to a HELLO block.
Definition: hello-uri.c:1223

References expiration, GNUNET_free, GNUNET_HELLO_dht_msg_to_block(), PeerInfo::id, ret, and t.

Here is the call graph for this function:

◆ handle_dht_p2p_hello()

static void handle_dht_p2p_hello ( void *  cls,
const struct GNUNET_MessageHeader hello 
)
static

Core handler for p2p HELLO messages.

Parameters
clsclosure
hellomessage

Definition at line 2578 of file gnunet-service-dht_neighbours.c.

2580{
2581 struct Target *t = cls;
2582 struct PeerInfo *peer = t->pi;
2583
2584 GNUNET_free (peer->hello);
2585 peer->hello_size = 0;
2588 &peer->id,
2589 &peer->hello,
2590 &peer->hello_size,
2591 &peer->hello_expiration));
2592}

References GNUNET_break, GNUNET_free, GNUNET_HELLO_dht_msg_to_block(), GNUNET_OK, PeerInfo::hello, PeerInfo::hello_expiration, PeerInfo::hello_size, PeerInfo::id, and t.

Here is the call graph for this function:

◆ GDS_u_receive()

void GDS_u_receive ( void *  cls,
void **  tctx,
void **  sctx,
const void *  message,
size_t  message_size 
)

Function to call when we receive a message.

Parameters
clsthe closure
[in,out]tctxctx of target address where we received the message from
[in,out]sctxctx of our own source address at which we received the message
messagethe message we received
message_sizenumber of bytes in message

Definition at line 2596 of file gnunet-service-dht_neighbours.c.

2601{
2602 struct Target *t = *tctx;
2603 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2604 GNUNET_MQ_hd_var_size (dht_p2p_get,
2606 struct PeerGetMessage,
2607 t),
2608 GNUNET_MQ_hd_var_size (dht_p2p_put,
2610 struct PeerPutMessage,
2611 t),
2612 GNUNET_MQ_hd_var_size (dht_p2p_result,
2614 struct PeerResultMessage,
2615 t),
2616 GNUNET_MQ_hd_var_size (dht_p2p_hello,
2618 struct GNUNET_MessageHeader,
2619 t),
2621 };
2622 const struct GNUNET_MessageHeader *mh = message;
2623
2624 (void) cls; /* the 'struct GDS_Underlay' */
2625 (void) sctx; /* our receiver address */
2626 if (NULL == t)
2627 {
2628 /* Received message claiming to originate from myself?
2629 Ignore! */
2630 GNUNET_break_op (0);
2631 return;
2632 }
2633 if (message_size < sizeof (*mh))
2634 {
2635 GNUNET_break_op (0);
2636 return;
2637 }
2638 if (message_size != ntohs (mh->size))
2639 {
2640 GNUNET_break_op (0);
2641 return;
2642 }
2644 "Handling message of type %u from peer %s\n",
2645 ntohs (mh->type),
2646 GNUNET_i2s (&t->pi->id));
2647 if (GNUNET_OK !=
2648 GNUNET_MQ_handle_message (core_handlers,
2649 mh))
2650 {
2651 GNUNET_break_op (0);
2652 return;
2653 }
2654}
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
enum GNUNET_GenericReturnValue GNUNET_MQ_handle_message(const struct GNUNET_MQ_MessageHandler *handlers, const struct GNUNET_MessageHeader *mh)
Call the message message handler that was registered for the type of the given message in the given h...
Definition: mq.c:205
#define GNUNET_MESSAGE_TYPE_DHT_P2P_HELLO
HELLO advertising a neighbours addresses.
#define GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
Peer is storing data in DHT.
Message handler for a specific message type.
Header for all communications.

References GNUNET_break_op, GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), GNUNET_log, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, GNUNET_MESSAGE_TYPE_DHT_P2P_HELLO, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, GNUNET_MQ_handle_message(), GNUNET_MQ_handler_end, GNUNET_MQ_hd_var_size, GNUNET_OK, mh, and t.

Referenced by load_underlay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_try_connect()

void GDS_try_connect ( void *  cls,
const struct GNUNET_PeerIdentity pid,
const char *  uri 
)

Callback function used to extract URIs from a builder.

Called when we should consider connecting to a peer.

Parameters
clsclosure pointing to a struct GNUNET_PeerIdentity *
urione of the URIs

Definition at line 2665 of file gnunet-service-dht_neighbours.c.

2668{
2669 struct GNUNET_HashCode phash;
2670 int peer_bucket;
2671 struct PeerBucket *bucket;
2672 (void) cls;
2673
2674 if (0 == GNUNET_memcmp (&GDS_my_identity,
2675 pid))
2676 {
2678 "Got a HELLO for my own PID, ignoring it\n");
2679 return; /* that's us! */
2680 }
2682 sizeof(*pid),
2683 &phash);
2684 peer_bucket = find_bucket (&phash);
2685 GNUNET_assert ( (peer_bucket >= 0) &&
2686 ((unsigned int) peer_bucket < MAX_BUCKETS));
2687 bucket = &k_buckets[peer_bucket];
2688 for (struct PeerInfo *pi = bucket->head;
2689 NULL != pi;
2690 pi = pi->next)
2691 if (0 ==
2692 GNUNET_memcmp (&pi->id,
2693 pid))
2694 {
2695 /* already connected */
2697 uri);
2698 return;
2699 }
2700 if (bucket->peers_size >= bucket_size)
2701 return; /* do not care */
2703 "Discovered peer %s at %s suitable for bucket %d (%u/%u), trying to connect\n",
2704 GNUNET_i2s (pid),
2705 uri,
2706 peer_bucket,
2707 bucket->peers_size,
2708 bucket_size);
2709 /* new peer that we like! */
2711 uri);
2712}
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
void GDS_u_try_connect(const struct GNUNET_PeerIdentity *pid, const char *address)
Ask all underlays to connect to peer pid at address.
@ GNUNET_ERROR_TYPE_INFO

References bucket_size, find_bucket(), GDS_my_identity, GDS_u_try_connect(), GNUNET_assert, GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_i2s(), GNUNET_log, GNUNET_memcmp, PeerBucket::head, k_buckets, MAX_BUCKETS, PeerInfo::next, PeerBucket::peers_size, pid, and uri.

Referenced by handle_dht_local_hello_offer(), and hello_check().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_NEIGHBOURS_broadcast()

void GDS_NEIGHBOURS_broadcast ( const struct GNUNET_MessageHeader msg)

Send msg to all peers in our buckets.

Parameters
msgmessage to broadcast

Definition at line 2721 of file gnunet-service-dht_neighbours.c.

2722{
2723 for (unsigned int bc = 0; bc<closest_bucket; bc++)
2724 {
2725 struct PeerBucket *bucket = &k_buckets[bc];
2726 unsigned int count = 0;
2727
2728 for (struct PeerInfo *pos = bucket->head;
2729 NULL != pos;
2730 pos = pos->next)
2731 {
2732 if (count >= bucket_size)
2733 break; /* we only consider first #bucket_size entries per bucket */
2734 count++;
2735 do_send (pos,
2736 msg);
2737 }
2738 }
2739}

References bucket_size, closest_bucket, do_send(), PeerBucket::head, k_buckets, msg, and PeerInfo::next.

Referenced by broadcast_hello().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_NEIGHBOURS_init()

enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_init ( void  )

Initialize neighbours subsystem.

Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 2743 of file gnunet-service-dht_neighbours.c.

2744{
2745
2746 unsigned long long temp_config_num;
2747
2750 "DHT",
2751 "DISABLE_TRY_CONNECT");
2752 if (GNUNET_OK ==
2754 "DHT",
2755 "bucket_size",
2756 &temp_config_num))
2757 bucket_size = (unsigned int) temp_config_num;
2760 "DHT",
2761 "CACHE_RESULTS");
2763 GNUNET_YES);
2764 return GNUNET_OK;
2765}
const struct GNUNET_CONFIGURATION_Handle * GDS_cfg
Configuration we use.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).

References all_connected_peers, bucket_size, cache_results, disable_try_connect, GDS_cfg, GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_CONTAINER_multipeermap_create(), GNUNET_OK, GNUNET_YES, and consensus-simulation::int.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_NEIGHBOURS_done()

void GDS_NEIGHBOURS_done ( void  )

Shutdown neighbours subsystem.

Definition at line 2769 of file gnunet-service-dht_neighbours.c.

2770{
2771 if (NULL == all_connected_peers)
2772 return;
2773 GNUNET_assert (0 ==
2776 all_connected_peers = NULL;
2777 GNUNET_assert (NULL == find_peer_task);
2778}
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.

References all_connected_peers, find_peer_task, GNUNET_assert, GNUNET_CONTAINER_multipeermap_destroy(), and GNUNET_CONTAINER_multipeermap_size().

Referenced by shutdown_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GDS_NEIGHBOURS_get_id()

struct GNUNET_PeerIdentity * GDS_NEIGHBOURS_get_id ( void  )

Get the ID of the local node.

Returns
identity of the local node

Definition at line 2782 of file gnunet-service-dht_neighbours.c.

2783{
2784 return &GDS_my_identity;
2785}

References GDS_my_identity.

Variable Documentation

◆ cache_results

int cache_results
static

Do we cache all results that we are routing in the local datacache?

Definition at line 350 of file gnunet-service-dht_neighbours.c.

Referenced by GDS_NEIGHBOURS_init(), and process_reply_with_path().

◆ closest_bucket

unsigned int closest_bucket
static

The lowest currently used bucket, initially 0 (for 0-bits matching bucket).

Definition at line 355 of file gnunet-service-dht_neighbours.c.

Referenced by GDS_am_closest_peer(), GDS_NEIGHBOURS_broadcast(), GDS_u_connect(), GDS_u_disconnect(), and select_peer().

◆ newly_found_peers

unsigned int newly_found_peers
static

How many peers have we added since we sent out our last find peer request?

Definition at line 361 of file gnunet-service-dht_neighbours.c.

Referenced by GDS_u_connect(), and send_find_peer_message().

◆ disable_try_connect

int disable_try_connect
static

Option for testing that disables the 'connect' function of the DHT.

Definition at line 366 of file gnunet-service-dht_neighbours.c.

Referenced by GDS_NEIGHBOURS_init(), GDS_u_connect(), GDS_u_disconnect(), and hello_check().

◆ k_buckets

struct PeerBucket k_buckets[sizeof(struct GNUNET_HashCode) *8]
static

The buckets.

Array of size MAX_BUCKETS. Offset 0 means 0 bits matching.

Definition at line 371 of file gnunet-service-dht_neighbours.c.

Referenced by GDS_am_closest_peer(), GDS_NEIGHBOURS_broadcast(), GDS_try_connect(), GDS_u_connect(), GDS_u_disconnect(), and select_peer().

◆ all_connected_peers

struct GNUNET_CONTAINER_MultiPeerMap* all_connected_peers
static

Hash map of all CORE-connected peers, for easy removal from k_buckets on disconnect.

Values are of type struct PeerInfo.

Definition at line 377 of file gnunet-service-dht_neighbours.c.

Referenced by GDS_NEIGHBOURS_done(), GDS_NEIGHBOURS_init(), GDS_NEIGHBOURS_lookup_peer(), GDS_u_connect(), GDS_u_disconnect(), get_target_peers(), and send_find_peer_message().

◆ bucket_size

◆ find_peer_task

struct GNUNET_SCHEDULER_Task* find_peer_task
static

Task that sends FIND PEER requests.

Definition at line 387 of file gnunet-service-dht_neighbours.c.

Referenced by GDS_NEIGHBOURS_done(), GDS_u_connect(), GDS_u_disconnect(), and send_find_peer_message().