From f9b01d9c8eeac932db6357426f8a70bf525a4e07 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 17 Jan 2017 17:18:53 +0100 Subject: [PATCH] use new shortmap to simplify CADET logic a bit --- src/cadet/cadet.h | 25 +----- src/cadet/cadet_common.c | 22 ----- src/cadet/cadet_protocol.h | 2 +- src/cadet/gnunet-cadet.c | 2 +- src/cadet/gnunet-service-cadet-new.c | 15 ++-- src/cadet/gnunet-service-cadet-new.h | 5 +- .../gnunet-service-cadet-new_connection.c | 49 ++---------- .../gnunet-service-cadet-new_connection.h | 22 ----- src/cadet/gnunet-service-cadet-new_core.c | 62 +++++++------- src/cadet/gnunet-service-cadet-new_peer.c | 28 +++---- src/cadet/gnunet-service-cadet-new_tunnels.c | 6 +- src/cadet/gnunet-service-cadet_connection.c | 80 +++++++++---------- src/cadet/gnunet-service-cadet_connection.h | 11 --- src/cadet/gnunet-service-cadet_local.c | 4 +- src/cadet/gnunet-service-cadet_peer.c | 53 ++++++------ src/cadet/gnunet-service-cadet_tunnel.c | 8 +- src/include/gnunet_cadet_service.h | 14 +--- src/include/gnunet_common.h | 15 +++- src/include/gnunet_crypto_lib.h | 23 +++--- src/util/common_logging.c | 23 ++++++ 20 files changed, 187 insertions(+), 282 deletions(-) diff --git a/src/cadet/cadet.h b/src/cadet/cadet.h index 163734c06..df8803662 100644 --- a/src/cadet/cadet.h +++ b/src/cadet/cadet.h @@ -375,26 +375,6 @@ uint32_t GC_min_pid (uint32_t a, uint32_t b); -/** - * Convert a 256 bit CadetHash into a 512 HashCode to use in GNUNET_h2s, - * multihashmap, and other HashCode-based functions. - * - * @param id A 256 bit hash to expand. - * - * @return A HashCode containing the original 256 bit hash right-padded with 0. - */ -const struct GNUNET_HashCode * -GC_h2hc (const struct GNUNET_CADET_Hash *id); - - -/** - * Get a string from a Cadet Hash (256 bits). - * WARNING: Not reentrant (based on GNUNET_h2s). - */ -const char * -GC_h2s (const struct GNUNET_CADET_Hash *id); - - /** * Allocate a string with a hexdump of any binary data. * @@ -405,7 +385,10 @@ GC_h2s (const struct GNUNET_CADET_Hash *id); * @return The size of the output. */ size_t -GC_bin2s (void *bin, unsigned int len, char **output); +GC_bin2s (void *bin, + unsigned int len, + char **output); + /** * Convert a message type into a string to help debug diff --git a/src/cadet/cadet_common.c b/src/cadet/cadet_common.c index 20ee7e5c9..9a0c58d48 100644 --- a/src/cadet/cadet_common.c +++ b/src/cadet/cadet_common.c @@ -86,28 +86,6 @@ GC_min_pid (uint32_t a, uint32_t b) } -const struct GNUNET_HashCode * -GC_h2hc (const struct GNUNET_CADET_Hash *id) -{ - static struct GNUNET_HashCode hc; - GNUNET_memcpy (&hc, id, sizeof (*id)); - - return &hc; -} - - -const char * -GC_h2s (const struct GNUNET_CADET_Hash *id) -{ - static char s[53]; - - GNUNET_memcpy (s, GNUNET_h2s_full (GC_h2hc (id)), 52); - s[52] = '\0'; - - return s; -} - - /** * Allocate a string with a hexdump of any binary data. * diff --git a/src/cadet/cadet_protocol.h b/src/cadet/cadet_protocol.h index e1f768ab7..c63337c95 100644 --- a/src/cadet/cadet_protocol.h +++ b/src/cadet/cadet_protocol.h @@ -290,7 +290,7 @@ struct GNUNET_CADET_Encrypted * Everything after this value will be encrypted with the header key * and authenticated. */ - struct GNUNET_CADET_Hash hmac; + struct GNUNET_ShortHashCode hmac; /**************** AX_HEADER start ****************/ diff --git a/src/cadet/gnunet-cadet.c b/src/cadet/gnunet-cadet.c index eeef2ff89..72d7bf8a9 100644 --- a/src/cadet/gnunet-cadet.c +++ b/src/cadet/gnunet-cadet.c @@ -742,7 +742,7 @@ tunnel_callback (void *cls, FPRINTF (stdout, "\t\t%X\n", ntohl (channels[i].cn)); FPRINTF (stdout, "\t%u connections\n", n_connections); for (i = 0; i < n_connections; i++) - FPRINTF (stdout, "\t\t%s\n", GC_h2s (&connections[i].connection_of_tunnel)); + FPRINTF (stdout, "\t\t%s\n", GNUNET_sh2s (&connections[i].connection_of_tunnel)); FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate)); FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate)); } diff --git a/src/cadet/gnunet-service-cadet-new.c b/src/cadet/gnunet-service-cadet-new.c index ad68f09b1..c8cd2fc63 100644 --- a/src/cadet/gnunet-service-cadet-new.c +++ b/src/cadet/gnunet-service-cadet-new.c @@ -172,9 +172,10 @@ struct GNUNET_CONTAINER_MultiHashMap *loose_channels; struct GNUNET_CONTAINER_MultiPeerMap *peers; /** - * Map from expanded connection hash codes to `struct CadetConnection` objects. + * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier` + * hash codes to `struct CadetConnection` objects. */ -struct GNUNET_CONTAINER_MultiHashMap *connections; +struct GNUNET_CONTAINER_MultiShortmap *connections; /** * How many messages are needed to trigger an AXOLOTL ratchet advance. @@ -335,7 +336,7 @@ shutdown_task (void *cls) } if (NULL != connections) { - GNUNET_CONTAINER_multihashmap_destroy (connections); + GNUNET_CONTAINER_multishortmap_destroy (connections); connections = NULL; } if (NULL != ats_ch) @@ -909,7 +910,7 @@ iter_channel (void *cls, struct CadetChannel *ch) { struct GNUNET_CADET_LocalInfoTunnel *msg = cls; - struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1]; + struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1]; struct GCT_ChannelTunnelNumber *chn = (struct GCT_ChannelTunnelNumber *) &h[msg->connections]; @@ -961,7 +962,7 @@ handle_show_tunnel (void *cls, ch_n = GCT_count_channels (t); c_n = GCT_count_any_connections (t); env = GNUNET_MQ_msg_extra (resp, - c_n * sizeof (struct GNUNET_CADET_Hash) + + c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) + ch_n * sizeof (struct GCT_ChannelTunnelNumber), GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL); resp->destination = msg->peer; @@ -1278,8 +1279,8 @@ run (void *cls, GNUNET_NO); peers = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_YES); - connections = GNUNET_CONTAINER_multihashmap_create (256, - GNUNET_YES); + connections = GNUNET_CONTAINER_multishortmap_create (256, + GNUNET_YES); GCH_init (c); GCD_init (c); GCO_init (c); diff --git a/src/cadet/gnunet-service-cadet-new.h b/src/cadet/gnunet-service-cadet-new.h index 903ceed94..3258a6666 100644 --- a/src/cadet/gnunet-service-cadet-new.h +++ b/src/cadet/gnunet-service-cadet-new.h @@ -144,9 +144,10 @@ extern struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key; extern struct GNUNET_CONTAINER_MultiHashMap *open_ports; /** - * Map from expanded connection hash codes to `struct CadetConnection` objects. + * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier` + * hash codes to `struct CadetConnection` objects. */ -extern struct GNUNET_CONTAINER_MultiHashMap *connections; +extern struct GNUNET_CONTAINER_MultiShortmap *connections; /** * Map from ports to channels where the ports were closed at the diff --git a/src/cadet/gnunet-service-cadet-new_connection.c b/src/cadet/gnunet-service-cadet-new_connection.c index 761bc403a..6b802b69a 100644 --- a/src/cadet/gnunet-service-cadet-new_connection.c +++ b/src/cadet/gnunet-service-cadet-new_connection.c @@ -196,47 +196,14 @@ GCC_destroy (struct CadetConnection *cc) GCPP_del_connection (cc->path, cc->off, cc); + GNUNET_assert (GNUNET_YES == + GNUNET_CONTAINER_multishortmap_remove (connections, + &GCC_get_id (cc)->connection_of_tunnel, + cc)); GNUNET_free (cc); } -/** - * Expand the shorter CADET hash to a full GNUnet hash. - * - * @param id hash to expand - * @return expanded hash - * @param deprecated - */ -const struct GNUNET_HashCode * -GCC_h2hc (const struct GNUNET_CADET_Hash *id) -{ - static struct GNUNET_HashCode hc; - char *ptr = (char *) &hc; - - GNUNET_assert (sizeof (hc) == 2 * sizeof (*id)); - GNUNET_memcpy (ptr, - id, - sizeof (*id)); - GNUNET_memcpy (&ptr[sizeof (*id)], - id, - sizeof (*id)); - return &hc; -} - - -/** - * Get the connection ID as a full hash. - * - * @param cc Connection to get the ID from. - * @return full hash ID of the connection. - */ -const struct GNUNET_HashCode * -GCC_get_h (const struct CadetConnection *cc) -{ - return GCC_h2hc (&cc->cid.connection_of_tunnel); -} - - /** * Return the tunnel associated with this connection. * @@ -429,10 +396,10 @@ GCC_create (struct CadetPeer *destination, &cc->cid, sizeof (cc->cid)); GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (connections, - GCC_get_h (cc), - cc, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); + GNUNET_CONTAINER_multishortmap_put (connections, + &GCC_get_id (cc)->connection_of_tunnel, + cc, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); cc->ready_cb = ready_cb; cc->ready_cb_cls = ready_cb_cls; cc->path = path; diff --git a/src/cadet/gnunet-service-cadet-new_connection.h b/src/cadet/gnunet-service-cadet-new_connection.h index 6ed83575f..515863aad 100644 --- a/src/cadet/gnunet-service-cadet-new_connection.h +++ b/src/cadet/gnunet-service-cadet-new_connection.h @@ -151,28 +151,6 @@ const struct GNUNET_CADET_ConnectionTunnelIdentifier * GCC_get_id (struct CadetConnection *cc); -/** - * Get the connection ID as a full hash. - * - * @param cc Connection to get the ID from. - * @return full hash ID of the connection. - * @deprecated try to replace use of full hash codes eventually... - */ -const struct GNUNET_HashCode * -GCC_get_h (const struct CadetConnection *cc); - - -/** - * Expand the shorter CADET hash to a full GNUnet hash. - * - * @param id hash to expand - * @return expanded hash - * @param deprecated - */ -const struct GNUNET_HashCode * -GCC_h2hc (const struct GNUNET_CADET_Hash *id); - - /** * Log connection info. * diff --git a/src/cadet/gnunet-service-cadet-new_core.c b/src/cadet/gnunet-service-cadet-new_core.c index 8cc6c843e..3980f2e2d 100644 --- a/src/cadet/gnunet-service-cadet-new_core.c +++ b/src/cadet/gnunet-service-cadet-new_core.c @@ -77,7 +77,7 @@ static struct GNUNET_CORE_Handle *core; /** * Routes on which this peer is an intermediate. */ -static struct GNUNET_CONTAINER_MultiHashMap *routes; +static struct GNUNET_CONTAINER_MultiShortmap *routes; /** @@ -86,10 +86,10 @@ static struct GNUNET_CONTAINER_MultiHashMap *routes; * @param cid hash generated from the connection identifier */ static struct CadetRoute * -get_route (const struct GNUNET_HashCode *cid) +get_route (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) { - return GNUNET_CONTAINER_multihashmap_get (routes, - cid); + return GNUNET_CONTAINER_multishortmap_get (routes, + &cid->connection_of_tunnel); } @@ -104,7 +104,7 @@ get_route (const struct GNUNET_HashCode *cid) */ static void route_message (struct CadetPeer *prev, - const struct GNUNET_HashCode *cid, /* FIXME: bad type... */ + const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, const struct GNUNET_MessageHeader *msg) { struct CadetRoute *route; @@ -117,10 +117,7 @@ route_message (struct CadetPeer *prev, env = GNUNET_MQ_msg (bm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN); - /* FIXME: ugly */ - memcpy (&bm->cid, - cid, - sizeof (bm->cid)); + bm->cid = *cid; bm->peer1 = my_full_id; GCP_send (prev, env); @@ -201,12 +198,11 @@ handle_connection_ack (void *cls, const struct GNUNET_CADET_ConnectionACK *msg) { struct CadetPeer *peer = cls; - const struct GNUNET_HashCode *cid = GCC_h2hc (&msg->cid.connection_of_tunnel); struct CadetConnection *cc; /* First, check if ACK belongs to a connection that ends here. */ - cc = GNUNET_CONTAINER_multihashmap_get (connections, - cid); + cc = GNUNET_CONTAINER_multishortmap_get (connections, + &msg->cid.connection_of_tunnel); if (NULL != cc) { /* verify ACK came from the right direction */ @@ -226,7 +222,7 @@ handle_connection_ack (void *cls, /* We're just an intermediary peer, route the message along its path */ route_message (peer, - cid, + &msg->cid, &msg->header); } @@ -243,13 +239,12 @@ handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBroken *msg) { struct CadetPeer *peer = cls; - const struct GNUNET_HashCode *cid = GCC_h2hc (&msg->cid.connection_of_tunnel); struct CadetConnection *cc; struct CadetRoute *route; /* First, check if message belongs to a connection that ends here. */ - cc = GNUNET_CONTAINER_multihashmap_get (connections, - cid); + cc = GNUNET_CONTAINER_multishortmap_get (connections, + &msg->cid.connection_of_tunnel); if (NULL != cc) { /* verify message came from the right direction */ @@ -268,9 +263,9 @@ handle_broken (void *cls, } /* We're just an intermediary peer, route the message along its path */ - route = get_route (cid); + route = get_route (&msg->cid); route_message (peer, - cid, + &msg->cid, &msg->header); destroy_route (route); } @@ -287,13 +282,12 @@ handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroy *msg) { struct CadetPeer *peer = cls; - const struct GNUNET_HashCode *cid = GCC_h2hc (&msg->cid.connection_of_tunnel); struct CadetConnection *cc; struct CadetRoute *route; /* First, check if message belongs to a connection that ends here. */ - cc = GNUNET_CONTAINER_multihashmap_get (connections, - cid); + cc = GNUNET_CONTAINER_multishortmap_get (connections, + &msg->cid.connection_of_tunnel); if (NULL != cc) { /* verify message came from the right direction */ @@ -312,9 +306,9 @@ handle_destroy (void *cls, } /* We're just an intermediary peer, route the message along its path */ - route = get_route (cid); + route = get_route (&msg->cid); route_message (peer, - cid, + &msg->cid, &msg->header); destroy_route (route); } @@ -369,12 +363,11 @@ handle_kx (void *cls, const struct GNUNET_CADET_KX *msg) { struct CadetPeer *peer = cls; - const struct GNUNET_HashCode *cid = GCC_h2hc (&msg->cid.connection_of_tunnel); struct CadetConnection *cc; /* First, check if message belongs to a connection that ends here. */ - cc = GNUNET_CONTAINER_multihashmap_get (connections, - cid); + cc = GNUNET_CONTAINER_multishortmap_get (connections, + &msg->cid.connection_of_tunnel); if (NULL != cc) { /* verify message came from the right direction */ @@ -395,7 +388,7 @@ handle_kx (void *cls, /* We're just an intermediary peer, route the message along its path */ route_message (peer, - cid, + &msg->cid, &msg->header); } @@ -427,12 +420,11 @@ handle_encrypted (void *cls, const struct GNUNET_CADET_Encrypted *msg) { struct CadetPeer *peer = cls; - const struct GNUNET_HashCode *cid = GCC_h2hc (&msg->cid.connection_of_tunnel); struct CadetConnection *cc; /* First, check if message belongs to a connection that ends here. */ - cc = GNUNET_CONTAINER_multihashmap_get (connections, - cid); + cc = GNUNET_CONTAINER_multishortmap_get (connections, + &msg->cid.connection_of_tunnel); if (NULL != cc) { /* verify message came from the right direction */ @@ -453,7 +445,7 @@ handle_encrypted (void *cls, /* We're just an intermediary peer, route the message along its path */ route_message (peer, - cid, + &msg->cid, &msg->header); } @@ -572,8 +564,8 @@ GCO_init (const struct GNUNET_CONFIGURATION_Handle *c) GNUNET_MQ_handler_end () }; - routes = GNUNET_CONTAINER_multihashmap_create (1024, - GNUNET_NO); + routes = GNUNET_CONTAINER_multishortmap_create (1024, + GNUNET_NO); core = GNUNET_CORE_connect (c, NULL, &core_init_cb, @@ -594,8 +586,8 @@ GCO_shutdown () GNUNET_CORE_disconnect (core); core = NULL; } - GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (routes)); - GNUNET_CONTAINER_multihashmap_destroy (routes); + GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes)); + GNUNET_CONTAINER_multishortmap_destroy (routes); } /* end of gnunet-cadet-service_core.c */ diff --git a/src/cadet/gnunet-service-cadet-new_peer.c b/src/cadet/gnunet-service-cadet-new_peer.c index 5c3e6cf17..55a03a206 100644 --- a/src/cadet/gnunet-service-cadet-new_peer.c +++ b/src/cadet/gnunet-service-cadet-new_peer.c @@ -155,7 +155,7 @@ struct CadetPeer /** * Connections that go through this peer; indexed by tid. */ - struct GNUNET_CONTAINER_MultiHashMap *connections; + struct GNUNET_CONTAINER_MultiShortmap *connections; /** * Handle for core transmissions. @@ -227,7 +227,7 @@ destroy_peer (void *cls) GNUNET_assert (NULL == cp->t); GNUNET_assert (NULL == cp->core_mq); GNUNET_assert (0 == cp->path_dll_length); - GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (cp->connections)); + GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections)); GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (peers, &cp->pid, @@ -252,7 +252,7 @@ destroy_peer (void *cls) GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion); cp->connectivity_suggestion = NULL; } - GNUNET_CONTAINER_multihashmap_destroy (cp->connections); + GNUNET_CONTAINER_multishortmap_destroy (cp->connections); GNUNET_CONTAINER_heap_destroy (cp->path_heap); GNUNET_free_non_null (cp->hello); /* Peer should not be freed if paths exist; if there are no paths, @@ -385,7 +385,7 @@ consider_peer_destroy (struct CadetPeer *cp) return; /* still relevant! */ if (NULL != cp->core_mq) return; /* still relevant! */ - if (0 != GNUNET_CONTAINER_multihashmap_size (cp->connections)) + if (0 != GNUNET_CONTAINER_multishortmap_size (cp->connections)) return; /* still relevant! */ if (0 < GNUNET_CONTAINER_heap_get_size (cp->path_heap)) { @@ -563,10 +563,10 @@ GCP_add_connection (struct CadetPeer *cp, struct CadetConnection *cc) { GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (cp->connections, - GCC_get_h (cc), - cc, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); + GNUNET_CONTAINER_multishortmap_put (cp->connections, + &GCC_get_id (cc)->connection_of_tunnel, + cc, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); } @@ -581,9 +581,9 @@ GCP_remove_connection (struct CadetPeer *cp, struct CadetConnection *cc) { GNUNET_assert (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_remove (cp->connections, - GCC_get_h (cc), - cc)); + GNUNET_CONTAINER_multishortmap_remove (cp->connections, + &GCC_get_id (cc)->connection_of_tunnel, + cc)); } @@ -603,7 +603,7 @@ consider_peer_activate (struct CadetPeer *cp) GNUNET_SCHEDULER_cancel (cp->destroy_task); cp->destroy_task = NULL; } - if ( (0 == GNUNET_CONTAINER_multihashmap_size (cp->connections)) && + if ( (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections)) && (NULL == cp->t) ) { /* We're just on a path or directly connected; don't bother too much */ @@ -673,8 +673,8 @@ GCP_get (const struct GNUNET_PeerIdentity *peer_id, return NULL; cp = GNUNET_new (struct CadetPeer); cp->pid = *peer_id; - cp->connections = GNUNET_CONTAINER_multihashmap_create (32, - GNUNET_YES); + cp->connections = GNUNET_CONTAINER_multishortmap_create (32, + GNUNET_YES); cp->path_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_put (peers, diff --git a/src/cadet/gnunet-service-cadet-new_tunnels.c b/src/cadet/gnunet-service-cadet-new_tunnels.c index b5a4e0112..01511d65e 100644 --- a/src/cadet/gnunet-service-cadet-new_tunnels.c +++ b/src/cadet/gnunet-service-cadet-new_tunnels.c @@ -534,7 +534,7 @@ t_hmac (const void *plaintext, size_t size, uint32_t iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *key, - struct GNUNET_CADET_Hash *hmac) + struct GNUNET_ShortHashCode *hmac) { static const char ctx[] = "cadet authentication key"; struct GNUNET_CRYPTO_AuthKey auth_key; @@ -834,7 +834,7 @@ try_old_ax_keys (struct CadetTunnel *t, size_t size) { struct CadetTunnelSkippedKey *key; - struct GNUNET_CADET_Hash *hmac; + struct GNUNET_ShortHashCode *hmac; struct GNUNET_CRYPTO_SymmetricInitializationVector iv; struct GNUNET_CADET_Encrypted plaintext_header; struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK; @@ -1012,7 +1012,7 @@ t_ax_decrypt_and_validate (struct CadetTunnel *t, size_t size) { struct CadetTunnelAxolotl *ax; - struct GNUNET_CADET_Hash msg_hmac; + struct GNUNET_ShortHashCode msg_hmac; struct GNUNET_HashCode hmac; struct GNUNET_CADET_Encrypted plaintext_header; uint32_t Np; diff --git a/src/cadet/gnunet-service-cadet_connection.c b/src/cadet/gnunet-service-cadet_connection.c index 2c21a1686..500511ce4 100644 --- a/src/cadet/gnunet-service-cadet_connection.c +++ b/src/cadet/gnunet-service-cadet_connection.c @@ -322,7 +322,7 @@ extern struct GNUNET_PeerIdentity my_full_id; /** * Connections known, indexed by cid (CadetConnection). */ -static struct GNUNET_CONTAINER_MultiHashMap *connections; +static struct GNUNET_CONTAINER_MultiShortmap *connections; /** * How many connections are we willing to maintain. @@ -475,7 +475,8 @@ fc_init (struct CadetFlowControl *fc) static struct CadetConnection * connection_get (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) { - return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (&cid->connection_of_tunnel)); + return GNUNET_CONTAINER_multishortmap_get (connections, + &cid->connection_of_tunnel); } @@ -912,7 +913,7 @@ check_neighbours (const struct CadetConnection *c) */ static int check_connection (void *cls, - const struct GNUNET_HashCode *key, + const struct GNUNET_ShortHashCode *key, void *value) { struct CadetConnection *c = value; @@ -932,9 +933,9 @@ GCC_check_connections () return; if (NULL == connections) return; - GNUNET_CONTAINER_multihashmap_iterate (connections, - &check_connection, - NULL); + GNUNET_CONTAINER_multishortmap_iterate (connections, + &check_connection, + NULL); } @@ -1109,7 +1110,7 @@ send_broken_unknown (const struct GNUNET_CADET_ConnectionTunnelIdentifier *conne GCC_check_connections (); LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n", - GNUNET_h2s (GC_h2hc (&connection_id->connection_of_tunnel))); + GNUNET_sh2s (&connection_id->connection_of_tunnel)); msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken)); msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN); @@ -1855,9 +1856,13 @@ log_message (const struct GNUNET_MessageHeader *message, default: arrow = "--"; } - LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n", - arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (&conn_id->connection_of_tunnel)), - GCP_2s(peer), (unsigned int) size); + LOG (GNUNET_ERROR_TYPE_INFO, + "<%s %s on conn %s from %s, %6u bytes\n", + arrow, + GC_m2s (type), + GNUNET_sh2s (&conn_id->connection_of_tunnel), + GCP_2s(peer), + (unsigned int) size); } /******************************************************************************/ @@ -2354,7 +2359,7 @@ GCC_handle_poll (struct CadetPeer *peer, GNUNET_NO); LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL message on unknown connection %s!\n", - GNUNET_h2s (GC_h2hc (&msg->cid.connection_of_tunnel))); + GNUNET_sh2s (&msg->cid.connection_of_tunnel)); send_broken_unknown (&msg->cid, &my_full_id, NULL, @@ -2428,7 +2433,7 @@ check_message (const struct GNUNET_MessageHeader *message, LOG (GNUNET_ERROR_TYPE_DEBUG, "%s on unknown connection %s\n", GC_m2s (ntohs (message->type)), - GNUNET_h2s (GC_h2hc (&cid->connection_of_tunnel))); + GNUNET_sh2s (&cid->connection_of_tunnel)); GNUNET_break_op (0); send_broken_unknown (cid, &my_full_id, @@ -2680,7 +2685,8 @@ GCC_init (const struct GNUNET_CONFIGURATION_Handle *c) } create_connection_time = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, refresh_connection_time); - connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO); + connections = GNUNET_CONTAINER_multishortmap_create (1024, + GNUNET_YES); } @@ -2695,7 +2701,7 @@ GCC_init (const struct GNUNET_CONFIGURATION_Handle *c) */ static int shutdown_iterator (void *cls, - const struct GNUNET_HashCode *key, + const struct GNUNET_ShortHashCode *key, void *value) { struct CadetConnection *c = value; @@ -2714,10 +2720,10 @@ GCC_shutdown (void) { LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n"); GCC_check_connections (); - GNUNET_CONTAINER_multihashmap_iterate (connections, - &shutdown_iterator, - NULL); - GNUNET_CONTAINER_multihashmap_destroy (connections); + GNUNET_CONTAINER_multishortmap_iterate (connections, + &shutdown_iterator, + NULL); + GNUNET_CONTAINER_multishortmap_destroy (connections); connections = NULL; } @@ -2748,9 +2754,10 @@ GCC_new (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, c = GNUNET_new (struct CadetConnection); c->id = *cid; GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (connections, - GCC_get_h (c), c, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); + GNUNET_CONTAINER_multishortmap_put (connections, + &c->id.connection_of_tunnel, + c, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); fc_init (&c->fwd_fc); fc_init (&c->bck_fc); c->fwd_fc.c = c; @@ -2851,9 +2858,9 @@ GCC_destroy (struct CadetConnection *c) if (GNUNET_NO == c->was_removed) { GNUNET_break (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_remove (connections, - GCC_get_h (c), - c)); + GNUNET_CONTAINER_multishortmap_remove (connections, + &c->id.connection_of_tunnel, + c)); } GNUNET_STATISTICS_update (stats, "# connections", @@ -2878,20 +2885,6 @@ GCC_get_id (const struct CadetConnection *c) } -/** - * Get the connection ID. - * - * @param c Connection to get the ID from. - * - * @return ID of the connection. - */ -const struct GNUNET_HashCode * -GCC_get_h (const struct CadetConnection *c) -{ - return GC_h2hc (&c->id.connection_of_tunnel); -} - - /** * Get the connection path. * @@ -3092,9 +3085,9 @@ GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer) GNUNET_assert (GNUNET_NO == c->was_removed); c->was_removed = GNUNET_YES; GNUNET_break (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_remove (connections, - GCC_get_h (c), - c)); + GNUNET_CONTAINER_multishortmap_remove (connections, + &c->id.connection_of_tunnel, + c)); /* Cancel queue in the direction that just died. */ connection_cancel_queues (c, ! fwd); GCC_stop_poll (c, ! fwd); @@ -3559,10 +3552,11 @@ GCC_2s (const struct CadetConnection *c) static char buf[128]; SPRINTF (buf, "%s (->%s)", - GNUNET_h2s (GC_h2hc (&GCC_get_id (c)->connection_of_tunnel)), GCT_2s (c->t)); + GNUNET_sh2s (&GCC_get_id (c)->connection_of_tunnel), + GCT_2s (c->t)); return buf; } - return GNUNET_h2s (GC_h2hc (&c->id.connection_of_tunnel)); + return GNUNET_sh2s (&c->id.connection_of_tunnel); } diff --git a/src/cadet/gnunet-service-cadet_connection.h b/src/cadet/gnunet-service-cadet_connection.h index f1eb4d59c..3d3e8adbe 100644 --- a/src/cadet/gnunet-service-cadet_connection.h +++ b/src/cadet/gnunet-service-cadet_connection.h @@ -304,17 +304,6 @@ const struct GNUNET_CADET_ConnectionTunnelIdentifier * GCC_get_id (const struct CadetConnection *c); -/** - * Get a hash for the connection ID. - * - * @param c Connection to get the hash. - * - * @return Hash expanded from the ID of the connection. - */ -const struct GNUNET_HashCode * -GCC_get_h (const struct CadetConnection *c); - - /** * Get the connection path. * diff --git a/src/cadet/gnunet-service-cadet_local.c b/src/cadet/gnunet-service-cadet_local.c index 6e6313902..cea28bcd2 100644 --- a/src/cadet/gnunet-service-cadet_local.c +++ b/src/cadet/gnunet-service-cadet_local.c @@ -976,7 +976,7 @@ static void iter_channel (void *cls, struct CadetChannel *ch) { struct GNUNET_CADET_LocalInfoTunnel *msg = cls; - struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1]; + struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1]; struct GNUNET_CADET_ChannelNumber *chn = (struct GNUNET_CADET_ChannelNumber *) &h[msg->connections]; chn[msg->channels] = GCCH_get_id (ch); @@ -1044,7 +1044,7 @@ handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client, c_n = GCT_count_any_connections (t); size = sizeof (struct GNUNET_CADET_LocalInfoTunnel); - size += c_n * sizeof (struct GNUNET_CADET_Hash); + size += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier); size += ch_n * sizeof (struct GNUNET_CADET_ChannelNumber); resp = GNUNET_malloc (size); diff --git a/src/cadet/gnunet-service-cadet_peer.c b/src/cadet/gnunet-service-cadet_peer.c index b2ad1124c..a3defb87e 100644 --- a/src/cadet/gnunet-service-cadet_peer.c +++ b/src/cadet/gnunet-service-cadet_peer.c @@ -165,7 +165,7 @@ struct CadetPeer /** * Connections that go through this peer; indexed by tid. */ - struct GNUNET_CONTAINER_MultiHashMap *connections; + struct GNUNET_CONTAINER_MultiShortmap *connections; /** * Handle for core transmissions. @@ -268,7 +268,7 @@ static int in_shutdown; */ static int notify_broken (void *cls, - const struct GNUNET_HashCode *key, + const struct GNUNET_ShortHashCode *key, void *value) { struct CadetPeer *peer = cls; @@ -373,7 +373,8 @@ core_connect_handler (void *cls, /* Create the connections hashmap */ GNUNET_assert (NULL == neighbor->connections); - neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO); + neighbor->connections = GNUNET_CONTAINER_multishortmap_create (16, + GNUNET_YES); GNUNET_STATISTICS_update (stats, "# peers", 1, @@ -425,11 +426,11 @@ core_disconnect_handler (void *cls, direct_path = pop_direct_path (p); if (NULL != p->connections) { - GNUNET_CONTAINER_multihashmap_iterate (p->connections, - ¬ify_broken, - p); - GNUNET_CONTAINER_multihashmap_destroy (p->connections); - p->connections = NULL; + GNUNET_CONTAINER_multishortmap_iterate (p->connections, + ¬ify_broken, + p); + GNUNET_CONTAINER_multishortmap_destroy (p->connections); + p->connections = NULL; } GNUNET_STATISTICS_update (stats, "# peers", @@ -800,8 +801,8 @@ peer_destroy (struct CadetPeer *peer) GCT_destroy_empty (peer->tunnel); if (NULL != peer->connections) { - GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections)); - GNUNET_CONTAINER_multihashmap_destroy (peer->connections); + GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (peer->connections)); + GNUNET_CONTAINER_multishortmap_destroy (peer->connections); peer->connections = NULL; } if (NULL != peer->hello_offer) @@ -1574,14 +1575,14 @@ GCP_add_connection (struct CadetPeer *peer, GCP_2s (peer)); GNUNET_assert (NULL != peer->connections); GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (peer->connections, - GCC_get_h (c), - c, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); + GNUNET_CONTAINER_multishortmap_put (peer->connections, + &GCC_get_id (c)->connection_of_tunnel, + c, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s has now %u connections.\n", GCP_2s (peer), - GNUNET_CONTAINER_multihashmap_size (peer->connections)); + GNUNET_CONTAINER_multishortmap_size (peer->connections)); } @@ -1792,13 +1793,13 @@ void GCP_check_connection (const struct CadetPeer *peer, const struct CadetConnection *c) { - GNUNET_assert (NULL != peer); - GNUNET_assert (NULL != peer->connections); - return; - GNUNET_assert (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_contains_value (peer->connections, - GCC_get_h (c), - c)); + GNUNET_assert (NULL != peer); + GNUNET_assert (NULL != peer->connections); + return; // ???? + GNUNET_assert (GNUNET_YES == + GNUNET_CONTAINER_multishortmap_contains_value (peer->connections, + &GCC_get_id (c)->connection_of_tunnel, + c)); } @@ -1822,13 +1823,13 @@ GCP_remove_connection (struct CadetPeer *peer, (NULL == peer->connections) ) return; GNUNET_assert (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_remove (peer->connections, - GCC_get_h (c), - c)); + GNUNET_CONTAINER_multishortmap_remove (peer->connections, + &GCC_get_id (c)->connection_of_tunnel, + c)); LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s remains with %u connections.\n", GCP_2s (peer), - GNUNET_CONTAINER_multihashmap_size (peer->connections)); + GNUNET_CONTAINER_multishortmap_size (peer->connections)); } diff --git a/src/cadet/gnunet-service-cadet_tunnel.c b/src/cadet/gnunet-service-cadet_tunnel.c index 29087dba4..09e91386e 100644 --- a/src/cadet/gnunet-service-cadet_tunnel.c +++ b/src/cadet/gnunet-service-cadet_tunnel.c @@ -649,7 +649,7 @@ new_ephemeral (struct CadetTunnel *t) static void t_hmac (const void *plaintext, size_t size, uint32_t iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *key, - struct GNUNET_CADET_Hash *hmac) + struct GNUNET_ShortHashCode *hmac) { static const char ctx[] = "cadet authentication key"; struct GNUNET_CRYPTO_AuthKey auth_key; @@ -916,7 +916,7 @@ try_old_ax_keys (struct CadetTunnel *t, void *dst, const struct GNUNET_CADET_Encrypted *src, size_t size) { struct CadetTunnelSkippedKey *key; - struct GNUNET_CADET_Hash *hmac; + struct GNUNET_ShortHashCode *hmac; struct GNUNET_CRYPTO_SymmetricInitializationVector iv; struct GNUNET_CADET_Encrypted plaintext_header; struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK; @@ -1096,7 +1096,7 @@ t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst, size_t size) { struct CadetTunnelAxolotl *ax; - struct GNUNET_CADET_Hash msg_hmac; + struct GNUNET_ShortHashCode msg_hmac; struct GNUNET_HashCode hmac; struct GNUNET_CADET_Encrypted plaintext_header; uint32_t Np; @@ -1406,7 +1406,7 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message, "Sending message of type %s with PID %u and CID %s\n", GC_m2s (type), htonl (ax_msg->pid), - GC_h2s (&ax_msg->cid.connection_of_tunnel)); + GNUNET_sh2s (&ax_msg->cid.connection_of_tunnel)); if (NULL == cont) { diff --git a/src/include/gnunet_cadet_service.h b/src/include/gnunet_cadet_service.h index c6b683370..c17cb983c 100644 --- a/src/include/gnunet_cadet_service.h +++ b/src/include/gnunet_cadet_service.h @@ -67,15 +67,6 @@ struct GNUNET_CADET_Channel; */ struct GNUNET_CADET_Port; -/** - * Hash to be used in Cadet communication. Only 256 bits needed, - * instead of the 512 from `struct GNUNET_HashCode`. - */ -struct GNUNET_CADET_Hash -{ - unsigned char bits[256 / 8]; -}; - /** * Channel options. Second line indicates filed in the @@ -255,8 +246,7 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle); struct GNUNET_CADET_Port * GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h, const struct GNUNET_HashCode *port, - GNUNET_CADET_InboundChannelNotificationHandler - new_channel, + GNUNET_CADET_InboundChannelNotificationHandler new_channel, void *new_channel_cls); /** @@ -496,7 +486,7 @@ typedef void */ struct GNUNET_CADET_ConnectionTunnelIdentifier { - struct GNUNET_CADET_Hash connection_of_tunnel; + struct GNUNET_ShortHashCode connection_of_tunnel; }; diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index 62ae499e2..9ad604711 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h @@ -556,6 +556,19 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls); +/** + * @ingroup logging + * Convert a short hash value to a string (for printing debug messages). + * This is one of the very few calls in the entire API that is + * NOT reentrant! + * + * @param shc the hash code + * @return string + */ +const char * +GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc); + + /** * @ingroup logging * Convert a hash value to a string (for printing debug messages). @@ -655,7 +668,7 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind); /** * @ingroup logging * Use this for fatal errors that cannot be handled - * + * * @param cond Condition to evaluate * @param comp Component string to use for logging */ diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index b6875c5cf..43fd32a58 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h @@ -51,20 +51,6 @@ extern "C" #endif #endif -/** - * @brief A 512-bit hashcode - */ -struct GNUNET_HashCode; - -/** - * The identity of the host (wraps the signing key of the peer). - */ -struct GNUNET_PeerIdentity; - -#include "gnunet_common.h" -#include - - /** * @brief A 512-bit hashcode. These are the default length for GNUnet, using SHA-512. */ @@ -85,6 +71,15 @@ struct GNUNET_ShortHashCode }; +/** + * The identity of the host (wraps the signing key of the peer). + */ +struct GNUNET_PeerIdentity; + +#include "gnunet_common.h" +#include + + /** * Maximum length of an ECC signature. * Note: round up to multiple of 8 minus 2 for alignment. diff --git a/src/util/common_logging.c b/src/util/common_logging.c index 5989db00c..ce229826e 100644 --- a/src/util/common_logging.c +++ b/src/util/common_logging.c @@ -1152,6 +1152,29 @@ GNUNET_h2s (const struct GNUNET_HashCode * hc) } +/** + * @ingroup logging + * Convert a short hash value to a string (for printing debug messages). + * This is one of the very few calls in the entire API that is + * NOT reentrant! + * + * @param shc the hash code + * @return string + */ +const char * +GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc) +{ + static char buf[32]; + + GNUNET_STRINGS_data_to_string (shc, + sizeof (*shc), + buf, + sizeof (buf)); + buf[6] = '\0'; + return (const char *) buf; +} + + /** * Convert a hash to a string (for printing debug messages). * This is one of the very few calls in the entire API that is -- 2.25.1