From 44efce8316b644f15b5e87852f5b7df7d58cfa97 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 3 Jan 2022 15:35:49 +0100 Subject: -more DHT code cleanup (no substantial changes) --- src/dht/gnunet-service-dht_clients.c | 4 +- src/dht/gnunet-service-dht_datacache.c | 2 +- src/dht/gnunet-service-dht_neighbours.c | 306 ++++++++++++++++---------------- src/dht/gnunet-service-dht_neighbours.h | 16 +- src/dht/gnunet-service-dht_routing.c | 29 ++- 5 files changed, 188 insertions(+), 169 deletions(-) (limited to 'src') diff --git a/src/dht/gnunet-service-dht_clients.c b/src/dht/gnunet-service-dht_clients.c index 28be0e9be..245130dbc 100644 --- a/src/dht/gnunet-service-dht_clients.c +++ b/src/dht/gnunet-service-dht_clients.c @@ -837,8 +837,8 @@ handle_dht_local_get_stop ( struct RemoveByUniqueIdContext ctx; GNUNET_STATISTICS_update (GDS_stats, - gettext_noop - ("# GET STOP requests received from clients"), 1, + "# GET STOP requests received from clients", + 1, GNUNET_NO); LOG (GNUNET_ERROR_TYPE_DEBUG, "Received GET STOP request for %s from local client %p\n", diff --git a/src/dht/gnunet-service-dht_datacache.c b/src/dht/gnunet-service-dht_datacache.c index 131cad8aa..214c4a3f4 100644 --- a/src/dht/gnunet-service-dht_datacache.c +++ b/src/dht/gnunet-service-dht_datacache.c @@ -237,7 +237,7 @@ GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key, if (NULL == datacache) return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; GNUNET_STATISTICS_update (GDS_stats, - gettext_noop ("# GET requests given to datacache"), + "# GET requests given to datacache", 1, GNUNET_NO); ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID; diff --git a/src/dht/gnunet-service-dht_neighbours.c b/src/dht/gnunet-service-dht_neighbours.c index 01a68b0b7..35502e0f5 100644 --- a/src/dht/gnunet-service-dht_neighbours.c +++ b/src/dht/gnunet-service-dht_neighbours.c @@ -1253,8 +1253,6 @@ get_target_peers (const struct GNUNET_HashCode *key, } -// FIXME-CG: REVIEW from here... - enum GNUNET_GenericReturnValue GDS_NEIGHBOURS_handle_put (const struct GDS_DATACACHE_BlockData *bd, enum GNUNET_DHT_RouteOption options, @@ -1263,13 +1261,8 @@ GDS_NEIGHBOURS_handle_put (const struct GDS_DATACACHE_BlockData *bd, struct GNUNET_CONTAINER_BloomFilter *bf) { unsigned int target_count; - unsigned int i; struct PeerInfo **targets; - struct PeerInfo *target; size_t msize; - struct GNUNET_MQ_Envelope *env; - struct PeerPutMessage *ppm; - struct GNUNET_PeerIdentity *pp; unsigned int skip_count; unsigned int put_path_length = bd->put_path_length; @@ -1314,14 +1307,14 @@ GDS_NEIGHBOURS_handle_put (const struct GDS_DATACACHE_BlockData *bd, GNUNET_free (targets); return GNUNET_NO; } - GNUNET_STATISTICS_update (GDS_stats, - "# PUT messages queued for transmission", - target_count, - GNUNET_NO); skip_count = 0; - for (i = 0; i < target_count; i++) + for (unsigned int i = 0; i < target_count; i++) { - target = targets[i]; + struct PeerInfo *target = targets[i]; + struct GNUNET_MQ_Envelope *env; + struct PeerPutMessage *ppm; + struct GNUNET_PeerIdentity *pp; + if (GNUNET_MQ_get_length (target->mq) >= MAXIMUM_PENDING_PER_PEER) { /* skip */ @@ -1365,6 +1358,10 @@ GDS_NEIGHBOURS_handle_put (const struct GDS_DATACACHE_BlockData *bd, env); } GNUNET_free (targets); + GNUNET_STATISTICS_update (GDS_stats, + "# PUT messages queued for transmission", + target_count - skip_count, + GNUNET_NO); return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO; } @@ -1382,11 +1379,7 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, { unsigned int target_count; struct PeerInfo **targets; - struct PeerInfo *target; - struct GNUNET_MQ_Envelope *env; size_t msize; - struct PeerGetMessage *pgm; - char *xq; size_t reply_bf_size; void *reply_bf; unsigned int skip_count; @@ -1436,21 +1429,22 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, GNUNET_free (targets); return GNUNET_NO; } - GNUNET_STATISTICS_update (GDS_stats, - "# GET messages queued for transmission", - target_count, - GNUNET_NO); /* forward request */ skip_count = 0; for (unsigned int i = 0; i < target_count; i++) { - target = targets[i]; + struct PeerInfo *target = targets[i]; + struct GNUNET_MQ_Envelope *env; + struct PeerGetMessage *pgm; + char *xq; + if (GNUNET_MQ_get_length (target->mq) >= MAXIMUM_PENDING_PER_PEER) { /* skip */ GNUNET_STATISTICS_update (GDS_stats, "# P2P messages dropped due to full queue", - 1, GNUNET_NO); + 1, + GNUNET_NO); skip_count++; continue; } @@ -1486,20 +1480,31 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, GNUNET_MQ_send (target->mq, env); } + GNUNET_STATISTICS_update (GDS_stats, + "# GET messages queued for transmission", + target_count - skip_count, + GNUNET_NO); GNUNET_free (targets); GNUNET_free (reply_bf); return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO; } +struct PeerInfo * +GDS_NEIGHBOURS_lookup_peer (const struct GNUNET_PeerIdentity *target) +{ + return GNUNET_CONTAINER_multipeermap_get (all_connected_peers, + target); +} + + void -GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, +GDS_NEIGHBOURS_handle_reply (struct PeerInfo *pi, const struct GDS_DATACACHE_BlockData *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, const struct GNUNET_PeerIdentity *get_path) { - struct PeerInfo *pi; struct GNUNET_MQ_Envelope *env; struct PeerResultMessage *prm; struct GNUNET_PeerIdentity *paths; @@ -1517,16 +1522,6 @@ GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, GNUNET_break (0); return; } - pi = GNUNET_CONTAINER_multipeermap_get (all_connected_peers, - target); - if (NULL == pi) - { - /* peer disconnected in the meantime, drop reply */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "No matching peer for reply for key %s\n", - GNUNET_h2s (query_hash)); - return; - } if (GNUNET_MQ_get_length (pi->mq) >= MAXIMUM_PENDING_PER_PEER) { /* skip */ @@ -1543,7 +1538,7 @@ GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Forwarding reply for key %s to peer %s\n", GNUNET_h2s (query_hash), - GNUNET_i2s (target)); + GNUNET_i2s (pi->id)); GNUNET_STATISTICS_update (GDS_stats, "# RESULT messages queued for transmission", 1, @@ -1572,7 +1567,7 @@ GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, /** - * To be called on core init/fail. + * To be called on core init. * * @param cls service closure * @param identity the public identity of this peer @@ -1587,7 +1582,7 @@ core_init (void *cls, GNUNET_i2s (identity)); my_identity = *identity; GNUNET_CRYPTO_hash (identity, - sizeof(struct GNUNET_PeerIdentity), + sizeof(struct GNUNET_PeerIdentity), &my_identity_hash); GNUNET_SERVICE_resume (GDS_service); } @@ -1604,17 +1599,15 @@ static enum GNUNET_GenericReturnValue check_dht_p2p_put (void *cls, const struct PeerPutMessage *put) { - uint32_t putlen; - uint16_t msize; + uint16_t msize = ntohs (put->header.size); + uint32_t putlen = ntohl (put->put_path_length); (void) cls; - msize = ntohs (put->header.size); - putlen = ntohl (put->put_path_length); - if ((msize < - sizeof(struct PeerPutMessage) - + putlen * sizeof(struct GNUNET_PeerIdentity)) || - (putlen > - GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_PeerIdentity))) + if ( (msize < + sizeof(struct PeerPutMessage) + + putlen * sizeof(struct GNUNET_PeerIdentity)) || + (putlen > + GNUNET_MAX_MESSAGE_SIZE / sizeof(struct GNUNET_PeerIdentity)) ) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -1784,9 +1777,9 @@ handle_dht_p2p_put (void *cls, 0, NULL /* get path */); /* store locally */ - if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || - (GDS_am_closest_peer (&put->key, - bf))) + if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || + (GDS_am_closest_peer (&put->key, + bf)) ) GDS_DATACACHE_handle_put (&bd); { enum GNUNET_GenericReturnValue forwarded; @@ -1816,12 +1809,12 @@ handle_dht_p2p_put (void *cls, * We have received a FIND PEER request. Send matching * HELLOs back. * - * @param sender sender of the FIND PEER request + * @param pi sender of the FIND PEER request * @param key peers close to this key are desired * @param bg group for filtering peers */ static void -handle_find_peer (const struct GNUNET_PeerIdentity *sender, +handle_find_peer (struct PeerInfo *pi, const struct GNUNET_HashCode *query_hash, struct GNUNET_BLOCK_Group *bg) { @@ -1852,7 +1845,7 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender, bd.data, bd.data_size)) { - GDS_NEIGHBOURS_handle_reply (sender, + GDS_NEIGHBOURS_handle_reply (pi, &bd, query_hash, 0, NULL /* get path */); @@ -1925,7 +1918,7 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender, bd.key = peer->phash; bd.data = hello; bd.data_size = hello_size; - GDS_NEIGHBOURS_handle_reply (sender, + GDS_NEIGHBOURS_handle_reply (pi, &bd, query_hash, 0, NULL /* get path */); @@ -1956,7 +1949,7 @@ handle_local_result (void *cls, pp); GNUNET_free (pp); } - GDS_NEIGHBOURS_handle_reply (peer->id, + GDS_NEIGHBOURS_handle_reply (peer, bd, &bd->key, 0, NULL /* get path */); @@ -1974,13 +1967,11 @@ static enum GNUNET_GenericReturnValue check_dht_p2p_get (void *cls, const struct PeerGetMessage *get) { - uint32_t xquery_size; - uint16_t msize; + uint16_t msize = ntohs (get->header.size); + uint32_t xquery_size = ntohl (get->xquery_size); (void) cls; - msize = ntohs (get->header.size); - xquery_size = ntohl (get->xquery_size); - if (msize < sizeof(struct PeerGetMessage) + xquery_size) + if (msize < sizeof(*get) + xquery_size) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -2000,24 +1991,16 @@ handle_dht_p2p_get (void *cls, const struct PeerGetMessage *get) { struct PeerInfo *peer = cls; - uint32_t xquery_size; - size_t reply_bf_size; - uint16_t msize; - enum GNUNET_BLOCK_Type type; - enum GNUNET_DHT_RouteOption options; + uint16_t msize = ntohs (get->header.size); + uint32_t xquery_size = ntohl (get->xquery_size); + uint32_t hop_count = ntohl (get->hop_count); + size_t reply_bf_size = msize - (sizeof(*get) + xquery_size); + enum GNUNET_BLOCK_Type type = (enum GNUNET_BLOCK_Type) ntohl (get->type); + enum GNUNET_DHT_RouteOption options = (enum GNUNET_DHT_RouteOption) ntohl (get->options); enum GNUNET_BLOCK_ReplyEvaluationResult eval = GNUNET_BLOCK_REPLY_OK_MORE; - struct GNUNET_BLOCK_Group *bg; - struct GNUNET_CONTAINER_BloomFilter *peer_bf; - const void *xquery; - enum GNUNET_GenericReturnValue forwarded; + const void *xquery = (const void *) &get[1]; /* parse and validate message */ - msize = ntohs (get->header.size); - xquery_size = ntohl (get->xquery_size); - reply_bf_size = msize - (sizeof(struct PeerGetMessage) + xquery_size); - type = ntohl (get->type); - options = ntohl (get->options); - xquery = (const void *) &get[1]; GNUNET_STATISTICS_update (GDS_stats, "# P2P GET requests received", 1, @@ -2044,7 +2027,7 @@ handle_dht_p2p_get (void *cls, GNUNET_h2s (&get->key), GNUNET_i2s (peer->id), tmp, - ntohl (get->hop_count), + hop_count, GNUNET_CRYPTO_hash_count_leading_zeros (&pxor), GNUNET_CRYPTO_hash_count_leading_zeros (&mxor), ntohl (get->xquery_size), @@ -2062,93 +2045,104 @@ handle_dht_p2p_get (void *cls, GNUNET_break_op (0); return; } - peer_bf = GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, - DHT_BLOOM_SIZE, - GNUNET_CONSTANTS_BLOOMFILTER_K); - GNUNET_break_op (GNUNET_YES == - GNUNET_CONTAINER_bloomfilter_test (peer_bf, - &peer->phash)); - bg = GNUNET_BLOCK_group_create (GDS_block_context, - type, - get->bf_mutator, - xquery + xquery_size, - reply_bf_size, - "filter-size", - reply_bf_size, - NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "GET for %s at %s after %u hops\n", - GNUNET_h2s (&get->key), - GNUNET_i2s (&my_identity), - (unsigned int) ntohl (get->hop_count)); - /* local lookup (this may update the reply_bf) */ - if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || - (GDS_am_closest_peer (&get->key, - peer_bf)) ) + { - if ((0 != (options & GNUNET_DHT_RO_FIND_PEER))) + struct GNUNET_BLOCK_Group *bg; + struct GNUNET_CONTAINER_BloomFilter *peer_bf; + + peer_bf = GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, + DHT_BLOOM_SIZE, + GNUNET_CONSTANTS_BLOOMFILTER_K); + GNUNET_break_op (GNUNET_YES == + GNUNET_CONTAINER_bloomfilter_test (peer_bf, + &peer->phash)); + bg = GNUNET_BLOCK_group_create (GDS_block_context, + type, + get->bf_mutator, + xquery + xquery_size, + reply_bf_size, + "filter-size", + reply_bf_size, + NULL); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "GET for %s at %s after %u hops\n", + GNUNET_h2s (&get->key), + GNUNET_i2s (&my_identity), + (unsigned int) hop_count); + /* local lookup (this may update the reply_bf) */ + if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || + (GDS_am_closest_peer (&get->key, + peer_bf)) ) { - GNUNET_STATISTICS_update (GDS_stats, - "# P2P FIND PEER requests processed", - 1, - GNUNET_NO); - handle_find_peer (peer->id, - &get->key, - bg); + if ((0 != (options & GNUNET_DHT_RO_FIND_PEER))) + { + GNUNET_STATISTICS_update (GDS_stats, + "# P2P FIND PEER requests processed", + 1, + GNUNET_NO); + handle_find_peer (peer, + &get->key, + bg); + } + else + { + eval = GDS_DATACACHE_handle_get (&get->key, + type, + xquery, + xquery_size, + bg, + &handle_local_result, + peer); + } } else + { + GNUNET_STATISTICS_update (GDS_stats, + "# P2P GET requests ONLY routed", + 1, + GNUNET_NO); + } + + /* remember request for routing replies */ + GDS_ROUTING_add (peer->id, + type, + bg, /* bg now owned by routing, but valid at least until end of this function! */ + options, + &get->key, + xquery, + xquery_size); + + /* P2P forwarding */ { - eval = GDS_DATACACHE_handle_get (&get->key, - type, - xquery, - xquery_size, - bg, - &handle_local_result, - peer); + bool forwarded = false; + uint32_t desired_replication_level = ntohl (get->desired_replication_level); + + if (eval != GNUNET_BLOCK_REPLY_OK_LAST) + forwarded = (GNUNET_OK == + GDS_NEIGHBOURS_handle_get (type, + options, + desired_replication_level, + hop_count, + &get->key, + xquery, + xquery_size, + bg, + peer_bf)); + GDS_CLIENTS_process_get ( + options | + (forwarded + ? 0 + : GNUNET_DHT_RO_LAST_HOP), + type, + hop_count, + desired_replication_level, + 0, + NULL, + &get->key); } + /* clean up; note that 'bg' is owned by routing now! */ + GNUNET_CONTAINER_bloomfilter_free (peer_bf); } - else - { - GNUNET_STATISTICS_update (GDS_stats, - "# P2P GET requests ONLY routed", - 1, - GNUNET_NO); - } - - /* remember request for routing replies */ - GDS_ROUTING_add (peer->id, - type, - bg, /* bg now owned by routing, but valid at least until end of this function! */ - options, - &get->key, - xquery, - xquery_size); - - /* P2P forwarding */ - forwarded = GNUNET_NO; - if (eval != GNUNET_BLOCK_REPLY_OK_LAST) - forwarded = GDS_NEIGHBOURS_handle_get (type, - options, - ntohl ( - get->desired_replication_level), - ntohl (get->hop_count), - &get->key, - xquery, - xquery_size, - bg, - peer_bf); - GDS_CLIENTS_process_get (options - | ((GNUNET_OK == forwarded) - ? GNUNET_DHT_RO_LAST_HOP : 0), - type, - ntohl (get->hop_count), - ntohl (get->desired_replication_level), - 0, - NULL, - &get->key); - - /* clean up; note that 'bg' is owned by routing now! */ - GNUNET_CONTAINER_bloomfilter_free (peer_bf); } diff --git a/src/dht/gnunet-service-dht_neighbours.h b/src/dht/gnunet-service-dht_neighbours.h index ba7cc6055..28468c740 100644 --- a/src/dht/gnunet-service-dht_neighbours.h +++ b/src/dht/gnunet-service-dht_neighbours.h @@ -38,6 +38,18 @@ extern struct GNUNET_HashCode my_identity_hash; +struct PeerInfo; + +/** + * Lookup peer by peer's identity. + * + * @param target peer to look up + * @return NULL if we are not connected to @a target + */ +struct PeerInfo * +GDS_NEIGHBOURS_lookup_peer (const struct GNUNET_PeerIdentity *target); + + /** * Perform a PUT operation. Forwards the given request to other * peers. Does not store the data locally. Does not give the @@ -94,7 +106,7 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, * other peers waiting for it. Does not do local caching or * forwarding to local clients. * - * @param target neighbour that should receive the block (if still connected) + * @param pi neighbour that should receive the block * @param type type of the block * @param bd details about the reply * @param query_hash query that was used for the request @@ -102,7 +114,7 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, * @param get_path peers this reply has traversed so far (if tracked) */ void -GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, +GDS_NEIGHBOURS_handle_reply (struct PeerInfo *pi, const struct GDS_DATACACHE_BlockData *bd, const struct GNUNET_HashCode *query_hash, unsigned int get_path_length, diff --git a/src/dht/gnunet-service-dht_routing.c b/src/dht/gnunet-service-dht_routing.c index 8ba0c70ad..ec36eae75 100644 --- a/src/dht/gnunet-service-dht_routing.c +++ b/src/dht/gnunet-service-dht_routing.c @@ -179,14 +179,27 @@ process (void *cls, case GNUNET_BLOCK_REPLY_OK_MORE: case GNUNET_BLOCK_REPLY_OK_LAST: case GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED: - GNUNET_STATISTICS_update (GDS_stats, - "# Good REPLIES matched against routing table", - 1, - GNUNET_NO); - GDS_NEIGHBOURS_handle_reply (&rr->peer, - &bdx, - query_hash, - get_path_length, pc->get_path); + { + struct PeerInfo *pi; + + GNUNET_STATISTICS_update (GDS_stats, + "# Good REPLIES matched against routing table", + 1, + GNUNET_NO); + pi = GDS_NEIGHBOURS_lookup_peer (&rr->peer); + if (NULL == pi) + { + /* peer disconnected in the meantime, drop reply */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "No matching peer for reply for key %s\n", + GNUNET_h2s (query_hash)); + return GNUNET_OK; + } + GDS_NEIGHBOURS_handle_reply (pi, + &bdx, + query_hash, + get_path_length, pc->get_path); + } break; case GNUNET_BLOCK_REPLY_OK_DUPLICATE: GNUNET_STATISTICS_update (GDS_stats, -- cgit v1.2.3