From 32c6b50f5c00042069e563ca238080ac4b6f309e Mon Sep 17 00:00:00 2001 From: Bart Polot Date: Fri, 4 Oct 2013 15:08:12 +0000 Subject: [PATCH] - sync --- src/mesh/gnunet-service-mesh-enc.c | 260 +--------------------- src/mesh/gnunet-service-mesh_connection.c | 153 +++++++++++++ src/mesh/gnunet-service-mesh_dht.c | 196 +++++++++++++++- src/mesh/gnunet-service-mesh_local.c | 67 +++--- src/mesh/gnunet-service-mesh_peer.c | 176 ++++++++------- 5 files changed, 469 insertions(+), 383 deletions(-) diff --git a/src/mesh/gnunet-service-mesh-enc.c b/src/mesh/gnunet-service-mesh-enc.c index 4fef1905e..18b6460b7 100644 --- a/src/mesh/gnunet-service-mesh-enc.c +++ b/src/mesh/gnunet-service-mesh-enc.c @@ -496,31 +496,6 @@ GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s) } -/** - * Get string description for tunnel state. - * - * @param s Tunnel state. - * - * @return String representation. - */ -static const char * -GNUNET_MESH_DEBUG_CS2S (enum MeshTunnelState s) -{ - switch (s) - { - case MESH_CONNECTION_NEW: - return "MESH_CONNECTION_NEW"; - case MESH_CONNECTION_SENT: - return "MESH_CONNECTION_SENT"; - case MESH_CONNECTION_ACK: - return "MESH_CONNECTION_ACK"; - case MESH_CONNECTION_READY: - return "MESH_CONNECTION_READY"; - default: - return "MESH_CONNECTION_STATE_ERROR"; - } -} - /******************************************************************************/ @@ -913,91 +888,6 @@ send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf) return sizeof (struct GNUNET_MESH_ConnectionACK); } -/** - * Build a PeerPath from the paths returned from the DHT, reversing the paths - * to obtain a local peer -> destination path and interning the peer ids. - * - * @return Newly allocated and created path - */ -static struct MeshPeerPath * -path_build_from_dht (const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length) -{ - struct MeshPeerPath *p; - GNUNET_PEER_Id id; - int i; - - p = path_new (1); - p->peers[0] = myid; - GNUNET_PEER_change_rc (myid, 1); - i = get_path_length; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i); - for (i--; i >= 0; i--) - { - id = GNUNET_PEER_intern (&get_path[i]); - if (p->length > 0 && id == p->peers[p->length - 1]) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n"); - GNUNET_PEER_change_rc (id, -1); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n", - GNUNET_i2s (&get_path[i])); - p->length++; - p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length); - p->peers[p->length - 1] = id; - } - } - i = put_path_length; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i); - for (i--; i >= 0; i--) - { - id = GNUNET_PEER_intern (&put_path[i]); - if (id == myid) - { - /* PUT path went through us, so discard the path up until now and start - * from here to get a much shorter (and loop-free) path. - */ - path_destroy (p); - p = path_new (0); - } - if (p->length > 0 && id == p->peers[p->length - 1]) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n"); - GNUNET_PEER_change_rc (id, -1); - } - else - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n", - GNUNET_i2s (&put_path[i])); - p->length++; - p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length); - p->peers[p->length - 1] = id; - } - } -#if MESH_DEBUG - if (get_path_length > 0) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n", - GNUNET_i2s (&get_path[0])); - if (put_path_length > 0) - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n", - GNUNET_i2s (&put_path[0])); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n", - p->length); - for (i = 0; i < p->length; i++) - { - struct GNUNET_PeerIdentity peer_id; - - GNUNET_PEER_resolve (p->peers[i], &peer_id); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i], - GNUNET_i2s (&peer_id)); - } -#endif - return p; -} /** @@ -1907,39 +1797,15 @@ handle_decrypted (struct MeshTunnel2 *t, * Called on each result obtained for the DHT search. * * @param cls closure - * @param exp when will this value expire - * @param key key of the result - * @param get_path path of the get request - * @param get_path_length lenght of get_path - * @param put_path path of the put request - * @param put_path_length length of the put_path - * @param type type of the result - * @param size number of bytes in data - * @param data pointer to the result data + * @param path */ static void -dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp, - const struct GNUNET_HashCode * key, - const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length, enum GNUNET_BLOCK_Type type, - size_t size, const void *data) +search_handler (void *cls, struct MeshPeerPath *path) { - struct MeshPeer *peer = cls; - struct MeshPeerPath *p; struct MeshConnection *c; - struct GNUNET_PeerIdentity pi; unsigned int connection_count; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n"); - GNUNET_PEER_resolve (peer->id, &pi); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi)); - - p = path_build_from_dht (get_path, get_path_length, - put_path, put_path_length); - path_add_to_peers (p, GNUNET_NO); - path_destroy (p); + path_add_to_peers (path, GNUNET_NO); /* Count connections */ connection_count = GMC_count (peer->tunnel->connection_head); @@ -1957,126 +1823,6 @@ dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp, } - -/** - * Method called whenever a given peer connects. - * - * @param cls closure - * @param peer peer identity this notification is about - */ -static void -core_connect (void *cls, const struct GNUNET_PeerIdentity *peer) -{ - struct MeshPeer *pi; - struct MeshPeerPath *path; - - DEBUG_CONN ("Peer connected\n"); - DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id)); - pi = peer_get (peer); - if (myid == pi->id) - { - DEBUG_CONN (" (self)\n"); - path = path_new (1); - } - else - { - DEBUG_CONN (" %s\n", GNUNET_i2s (peer)); - path = path_new (2); - path->peers[1] = pi->id; - GNUNET_PEER_change_rc (pi->id, 1); - GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO); - } - path->peers[0] = myid; - GNUNET_PEER_change_rc (myid, 1); - peer_add_path (pi, path, GNUNET_YES); - - pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES); - return; -} - - -/** - * Method called whenever a peer disconnects. - * - * @param cls closure - * @param peer peer identity this notification is about - */ -static void -core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) -{ - struct MeshPeer *pi; - - DEBUG_CONN ("Peer disconnected\n"); - pi = GNUNET_CONTAINER_multipeermap_get (peers, peer); - if (NULL == pi) - { - GNUNET_break (0); - return; - } - - GNUNET_CONTAINER_multihashmap_iterate (pi->connections, - GMC_notify_broken, - pi); - GNUNET_CONTAINER_multihashmap_destroy (pi->connections); - pi->connections = NULL; - if (NULL != pi->core_transmit) - { - GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit); - pi->core_transmit = NULL; - } - if (myid == pi->id) - { - DEBUG_CONN (" (self)\n"); - } - GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO); - - return; -} - - - -/** - * To be called on core init/fail. - * - * @param cls Closure (config) - * @param identity the public identity of this peer - */ -static void -core_init (void *cls, - const struct GNUNET_PeerIdentity *identity) -{ - const struct GNUNET_CONFIGURATION_Handle *c = cls; - static int i = 0; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n"); - if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n")); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - " core id %s\n", - GNUNET_i2s (identity)); - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - " my id %s\n", - GNUNET_i2s (&my_full_id)); - GNUNET_CORE_disconnect (core_handle); - core_handle = GNUNET_CORE_connect (c, /* Main configuration */ - NULL, /* Closure passed to MESH functions */ - &core_init, /* Call core_init once connected */ - &core_connect, /* Handle connects */ - &core_disconnect, /* remove peers on disconnects */ - NULL, /* Don't notify about all incoming messages */ - GNUNET_NO, /* For header only in notification */ - NULL, /* Don't notify about all outbound messages */ - GNUNET_NO, /* For header-only out notification */ - core_handlers); /* Register these handlers */ - if (10 < i++) - GNUNET_abort(); - } - server_init (); - return; -} - - /******************************************************************************/ /************************ MAIN FUNCTIONS ****************************/ /******************************************************************************/ diff --git a/src/mesh/gnunet-service-mesh_connection.c b/src/mesh/gnunet-service-mesh_connection.c index b4bb8b279..dfe4fa953 100644 --- a/src/mesh/gnunet-service-mesh_connection.c +++ b/src/mesh/gnunet-service-mesh_connection.c @@ -31,6 +31,7 @@ #include "gnunet-service-mesh_connection.h" #include "gnunet-service-mesh_peer.h" +#include "gnunet-service-mesh_local.h" #include "mesh_protocol_enc.h" #include "mesh_path.h" @@ -286,6 +287,34 @@ static struct GNUNET_TIME_Relative refresh_connection_time; static struct GNUNET_CORE_Handle *core_handle; + +/** + * Get string description for tunnel state. + * + * @param s Tunnel state. + * + * @return String representation. + */ +static const char * +GMC_DEBUG_state2s (enum MeshTunnelState s) +{ + switch (s) + { + case MESH_CONNECTION_NEW: + return "MESH_CONNECTION_NEW"; + case MESH_CONNECTION_SENT: + return "MESH_CONNECTION_SENT"; + case MESH_CONNECTION_ACK: + return "MESH_CONNECTION_ACK"; + case MESH_CONNECTION_READY: + return "MESH_CONNECTION_READY"; + default: + return "MESH_CONNECTION_STATE_ERROR"; + } +} + + + /** * Initialize a Flow Control structure to the initial state. * @@ -1483,6 +1512,130 @@ connection_reset_timeout (struct MeshConnection *c, int fwd) + + +/** + * Method called whenever a given peer connects. + * + * @param cls closure + * @param peer peer identity this notification is about + */ +static void +core_connect (void *cls, const struct GNUNET_PeerIdentity *peer) +{ + struct MeshPeer *pi; + struct MeshPeerPath *path; + + DEBUG_CONN ("Peer connected\n"); + DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id)); + pi = peer_get (peer); + if (myid == pi->id) + { + DEBUG_CONN (" (self)\n"); + path = path_new (1); + } + else + { + DEBUG_CONN (" %s\n", GNUNET_i2s (peer)); + path = path_new (2); + path->peers[1] = pi->id; + GNUNET_PEER_change_rc (pi->id, 1); + GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO); + } + path->peers[0] = myid; + GNUNET_PEER_change_rc (myid, 1); + peer_add_path (pi, path, GNUNET_YES); + + pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES); + return; +} + + +/** + * Method called whenever a peer disconnects. + * + * @param cls closure + * @param peer peer identity this notification is about + */ +static void +core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) +{ + struct MeshPeer *pi; + + DEBUG_CONN ("Peer disconnected\n"); + pi = GNUNET_CONTAINER_multipeermap_get (peers, peer); + if (NULL == pi) + { + GNUNET_break (0); + return; + } + + GNUNET_CONTAINER_multihashmap_iterate (pi->connections, + GMC_notify_broken, + pi); + GNUNET_CONTAINER_multihashmap_destroy (pi->connections); + pi->connections = NULL; + if (NULL != pi->core_transmit) + { + GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit); + pi->core_transmit = NULL; + } + if (myid == pi->id) + { + DEBUG_CONN (" (self)\n"); + } + GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO); + + return; +} + + + +/** + * To be called on core init/fail. + * + * @param cls Closure (config) + * @param identity the public identity of this peer + */ +static void +core_init (void *cls, + const struct GNUNET_PeerIdentity *identity) +{ + const struct GNUNET_CONFIGURATION_Handle *c = cls; + static int i = 0; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n"); + if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id))) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n")); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + " core id %s\n", + GNUNET_i2s (identity)); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + " my id %s\n", + GNUNET_i2s (&my_full_id)); + GNUNET_CORE_disconnect (core_handle); + core_handle = GNUNET_CORE_connect (c, /* Main configuration */ + NULL, /* Closure passed to MESH functions */ + &core_init, /* Call core_init once connected */ + &core_connect, /* Handle connects */ + &core_disconnect, /* remove peers on disconnects */ + NULL, /* Don't notify about all incoming messages */ + GNUNET_NO, /* For header only in notification */ + NULL, /* Don't notify about all outbound messages */ + GNUNET_NO, /* For header-only out notification */ + core_handlers); /* Register these handlers */ + if (10 < i++) + GNUNET_abort(); + } + GML_start (); + return; +} + + + + + /** * Core handler for connection creation. * diff --git a/src/mesh/gnunet-service-mesh_dht.c b/src/mesh/gnunet-service-mesh_dht.c index 7f8450637..a194bd0b7 100644 --- a/src/mesh/gnunet-service-mesh_dht.c +++ b/src/mesh/gnunet-service-mesh_dht.c @@ -24,6 +24,7 @@ #include "gnunet_dht_service.h" +#include "mesh_path.h" #include "gnunet-service-mesh_dht.h" #include "gnunet-service-mesh_peer.h" @@ -35,11 +36,38 @@ #define DEBUG_DHT(...) #endif +#define LOG (level, ...) GNUNET_log_from ("mesh-dht", level, __VA_ARGS__) + + + +/** + * Callback called on each path found over the DHT. + * + * @param cls Closure. + * @param path An unchecked, unoptimized path to the target node. + * After callback will no longer be valid! + */ +typedef void (*GMD_search_callback) (void *cls, + const struct MeshPeerPath *path); + /******************************************************************************/ /******************************** STRUCTS **********************************/ /******************************************************************************/ +/** + * Handle for DHT searches. + */ +struct GMD_search_handle +{ + /** DHT_GET handle. */ + struct GNUNET_DHT_GetHandle *dhtget; + + /** Provided callback to call when a path is found. */ + GMD_search_callback callback; + /** Provided closure. */ + void *cls; +}; /******************************************************************************/ @@ -66,10 +94,15 @@ static unsigned long long dht_replication_level; */ static GNUNET_SCHEDULER_TaskIdentifier announce_id_task; +/** + * Own ID (short value). + */ +static GNUNET_PEER_Id short_id; + /** * Own ID (full value). */ -static struct GNUNET_PeerIdentity *id; +static struct GNUNET_PeerIdentity *full_id; /** * Own private key. @@ -82,6 +115,130 @@ static struct GNUNET_CRYPTO_EccPrivateKey *private_key; /******************************************************************************/ +/** + * Build a PeerPath from the paths returned from the DHT, reversing the paths + * to obtain a local peer -> destination path and interning the peer ids. + * + * @return Newly allocated and created path + */ +static struct MeshPeerPath * +path_build_from_dht (const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length) +{ + struct MeshPeerPath *p; + GNUNET_PEER_Id id; + int i; + + p = path_new (1); + p->peers[0] = myid; + GNUNET_PEER_change_rc (myid, 1); + i = get_path_length; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i); + for (i--; i >= 0; i--) + { + id = GNUNET_PEER_intern (&get_path[i]); + if (p->length > 0 && id == p->peers[p->length - 1]) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n"); + GNUNET_PEER_change_rc (id, -1); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n", + GNUNET_i2s (&get_path[i])); + p->length++; + p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length); + p->peers[p->length - 1] = id; + } + } + i = put_path_length; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i); + for (i--; i >= 0; i--) + { + id = GNUNET_PEER_intern (&put_path[i]); + if (id == myid) + { + /* PUT path went through us, so discard the path up until now and start + * from here to get a much shorter (and loop-free) path. + */ + path_destroy (p); + p = path_new (0); + } + if (p->length > 0 && id == p->peers[p->length - 1]) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n"); + GNUNET_PEER_change_rc (id, -1); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n", + GNUNET_i2s (&put_path[i])); + p->length++; + p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length); + p->peers[p->length - 1] = id; + } + } +#if MESH_DEBUG + if (get_path_length > 0) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n", + GNUNET_i2s (&get_path[0])); + if (put_path_length > 0) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n", + GNUNET_i2s (&put_path[0])); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n", + p->length); + for (i = 0; i < p->length; i++) + { + struct GNUNET_PeerIdentity peer_id; + + GNUNET_PEER_resolve (p->peers[i], &peer_id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i], + GNUNET_i2s (&peer_id)); + } +#endif + return p; +} + + +/** + * Function to process paths received for a new peer addition. The recorded + * paths form the initial tunnel, which can be optimized later. + * Called on each result obtained for the DHT search. + * + * @param cls closure + * @param exp when will this value expire + * @param key key of the result + * @param get_path path of the get request + * @param get_path_length lenght of get_path + * @param put_path path of the put request + * @param put_path_length length of the put_path + * @param type type of the result + * @param size number of bytes in data + * @param data pointer to the result data + */ +static void +dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp, + const struct GNUNET_HashCode * key, + const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length, enum GNUNET_BLOCK_Type type, + size_t size, const void *data) +{ + struct GMD_search_handle *h = cls; + struct MeshPeerPath *p; + + LOG (GNUNET_ERROR_TYPE_DEBUG, "Got results!\n"); + p = path_build_from_dht (get_path, get_path_length, + put_path, put_path_length); + h->callback (h->cls, p); + path_destroy (p); + return; +} + + /** * Periodically announce self id in the DHT * @@ -105,8 +262,8 @@ announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) */ DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (id)); - block.id = *id; - GNUNET_CRYPTO_hash (id, sizeof (struct GNUNET_PeerIdentity), &phash); + block.id = *full_id; + GNUNET_CRYPTO_hash (full_id, sizeof (struct GNUNET_PeerIdentity), &phash); GNUNET_DHT_put (dht_handle, /* DHT handle */ &phash, /* Key to use */ dht_replication_level, /* Replication level */ @@ -137,7 +294,7 @@ void GMD_init (const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_PeerIdentity *peer_id) { - id = peer_id; + full_id = peer_id; if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL", &dht_replication_level)) @@ -184,3 +341,34 @@ GMD_shutdown(void ) announce_id_task = GNUNET_SCHEDULER_NO_TASK; } } + +struct GMD_search_handle * +GMD_search (const struct GNUNET_PeerIdentity *peer_id, + GMD_search_callback callback, void *cls) +{ + struct GNUNET_HashCode phash; + struct GMD_search_handle *h; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + " Starting DHT GET for peer %s\n", GNUNET_i2s (peer_id)); + GNUNET_CRYPTO_hash (peer_id, sizeof (struct GNUNET_PeerIdentity), &phash); + h = GNUNET_new (struct GMD_search_handle); + h->cls = cls; + h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */ + GNUNET_BLOCK_TYPE_MESH_PEER, /* type */ + &phash, /* key to search */ + dht_replication_level, /* replication level */ + GNUNET_DHT_RO_RECORD_ROUTE | + GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, + NULL, /* xquery */ + 0, /* xquery bits */ + &dht_get_id_handler, h); + return h; +} + +void +GMD_search_stop (struct GMD_search_handle *h) +{ + GNUNET_DHT_get_stop (h->dhtget); + GNUNET_free (h); +} \ No newline at end of file diff --git a/src/mesh/gnunet-service-mesh_local.c b/src/mesh/gnunet-service-mesh_local.c index 989def211..764ebd68f 100644 --- a/src/mesh/gnunet-service-mesh_local.c +++ b/src/mesh/gnunet-service-mesh_local.c @@ -157,8 +157,8 @@ client_release_ports (void *cls, * @param cls Closure (unused). * @param client Client handler. */ -void -GMLH_client_connect (void *cls, struct GNUNET_SERVER_Client *client) +static void +handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client) { struct MeshClient *c; @@ -180,8 +180,8 @@ GMLH_client_connect (void *cls, struct GNUNET_SERVER_Client *client) * @param client identification of the client; NULL * for the last call when the server is destroyed */ -void -GMLH_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) +static void +handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) { struct MeshClient *c; @@ -240,9 +240,9 @@ GMLH_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) * @param client identification of the client * @param message the actual message, which includes messages the client wants */ -void -GMLH_new_client (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_new_client (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct GNUNET_MESH_ClientConnect *cc_msg; struct MeshClient *c; @@ -306,9 +306,9 @@ GMLH_new_client (void *cls, struct GNUNET_SERVER_Client *client, * @param client Identification of the client. * @param message The actual message. */ -void -GMLH_channel_create (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct GNUNET_MESH_ChannelMessage *msg; struct MeshPeer *peer; @@ -409,9 +409,9 @@ GMLH_channel_create (void *cls, struct GNUNET_SERVER_Client *client, * @param client identification of the client * @param message the actual message */ -void -GMLH_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct GNUNET_MESH_ChannelMessage *msg; struct MeshClient *c; @@ -486,9 +486,9 @@ GMLH_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client, * @param client identification of the client * @param message the actual message */ -void -GMLH_data (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_data (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct GNUNET_MESH_LocalData *msg; struct MeshClient *c; @@ -584,9 +584,9 @@ GMLH_data (void *cls, struct GNUNET_SERVER_Client *client, * @param client Identification of the client. * @param message The actual message. */ -void -GMLH_ack (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_ack (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct GNUNET_MESH_LocalAck *msg; struct MeshChannelReliability *rel; @@ -677,9 +677,9 @@ GMLH_ack (void *cls, struct GNUNET_SERVER_Client *client, * @param client Identification of the client. * @param message The actual message. */ -void -GMLH_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +static void +handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { struct MeshClient *c; @@ -712,8 +712,8 @@ GMLH_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client, * @param message The actual message. */ void -GMLH_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { const struct GNUNET_MESH_LocalMonitor *msg; struct GNUNET_MESH_LocalMonitor *resp; @@ -768,17 +768,17 @@ GMLH_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client, * Functions to handle messages from clients */ static struct GNUNET_SERVER_MessageHandler client_handlers[] = { - {&GMLH_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0}, - {&GMLH_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE, + {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0}, + {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE, sizeof (struct GNUNET_MESH_ChannelMessage)}, - {&GMLH_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY, + {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY, sizeof (struct GNUNET_MESH_ChannelMessage)}, - {&GMLH_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, - {&GMLH_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, + {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, + {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, sizeof (struct GNUNET_MESH_LocalAck)}, - {&GMLH_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS, + {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS, sizeof (struct GNUNET_MessageHeader)}, - {&GMLH_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL, + {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL, sizeof (struct GNUNET_MESH_LocalMonitor)}, {NULL, NULL, 0, 0} }; @@ -802,6 +802,7 @@ GML_init (struct GNUNET_SERVER_Handle *handle) ports = GNUNET_CONTAINER_multihashmap32_create (32); } + /** * Install server (service) handlers and start listening to clients. */ @@ -809,8 +810,8 @@ void GML_start (void) { GNUNET_SERVER_add_handlers (server_handle, client_handlers); - GNUNET_SERVER_connect_notify (server_handle, &GMLH_client_connect, NULL); - GNUNET_SERVER_disconnect_notify (server_handle, &GMLH_client_disconnect, + GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL); + GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect, NULL); nc = GNUNET_SERVER_notification_context_create (server_handle, 1); diff --git a/src/mesh/gnunet-service-mesh_peer.c b/src/mesh/gnunet-service-mesh_peer.c index 13dc05c65..75decd3bb 100644 --- a/src/mesh/gnunet-service-mesh_peer.c +++ b/src/mesh/gnunet-service-mesh_peer.c @@ -23,6 +23,7 @@ #include "gnunet_util_lib.h" #include "gnunet-service-mesh_peer.h" +#include "gnunet-service-mesh_dht.h" #include "mesh_path.h" /******************************************************************************/ @@ -393,95 +394,6 @@ peer_get_best_path (const struct MeshPeer *peer) } - -/** - * Try to establish a new connection to this peer in the given tunnel. - * If the peer doesn't have any path to it yet, try to get one. - * If the peer already has some path, send a CREATE CONNECTION towards it. - * - * @param peer PeerInfo of the peer. - */ -static void -peer_connect (struct MeshPeer *peer) -{ - struct MeshTunnel2 *t; - struct MeshPeerPath *p; - struct MeshConnection *c; - int rerun_dhtget; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "peer_connect towards %s\n", - peer2s (peer)); - t = peer->tunnel; - c = NULL; - rerun_dhtget = GNUNET_NO; - - if (NULL != peer->path_head) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n"); - p = peer_get_best_path (peer); - if (NULL != p) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length); - c = tunnel_use_path (t, p); - if (NULL == c) - { - /* This case can happen when the path includes a first hop that is - * not yet known to be connected. - * - * This happens quite often during testing when running mesh - * under valgrind: core connect notifications come very late and the - * DHT result has already come and created a valid path. - * In this case, the peer->connections hashmap will be NULL and - * tunnel_use_path will not be able to create a connection from that - * path. - * - * Re-running the DHT GET should give core time to callback. - */ - GNUNET_break(0); - rerun_dhtget = GNUNET_YES; - } - else - { - send_connection_create (c); - return; - } - } - } - - if (NULL != peer->dhtget && GNUNET_YES == rerun_dhtget) - { - GNUNET_DHT_get_stop (peer->dhtget); - peer->dhtget = NULL; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " Stopping DHT GET for peer %s\n", peer2s (peer)); - } - - if (NULL == peer->dhtget) - { - const struct GNUNET_PeerIdentity *id; - struct GNUNET_HashCode phash; - - id = GNUNET_PEER_resolve2 (peer->id); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " Starting DHT GET for peer %s\n", peer2s (peer)); - GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &phash); - peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */ - GNUNET_BLOCK_TYPE_MESH_PEER, /* type */ - &phash, /* key to search */ - dht_replication_level, /* replication level */ - GNUNET_DHT_RO_RECORD_ROUTE | - GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, - NULL, /* xquery */ - 0, /* xquery bits */ - &dht_get_id_handler, peer); - if (MESH_TUNNEL_NEW == t->state) - tunnel_change_state (t, MESH_TUNNEL_SEARCHING); - } -} - - - /** * Add the path to the peer and update the path used to reach it in case this * is the shortest. @@ -622,3 +534,89 @@ GMP_shutdown (void) GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL); } + +/** + * Try to establish a new connection to this peer in the given tunnel. + * If the peer doesn't have any path to it yet, try to get one. + * If the peer already has some path, send a CREATE CONNECTION towards it. + * + * @param peer PeerInfo of the peer. + */ +void +GMP_connect (struct MeshPeer *peer) +{ + struct MeshTunnel2 *t; + struct MeshPeerPath *p; + struct MeshConnection *c; + int rerun_dhtget; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "peer_connect towards %s\n", + peer2s (peer)); + t = peer->tunnel; + c = NULL; + rerun_dhtget = GNUNET_NO; + + if (NULL != peer->path_head) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "path exists\n"); + p = peer_get_best_path (peer); + if (NULL != p) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length); + c = tunnel_use_path (t, p); + if (NULL == c) + { + /* This case can happen when the path includes a first hop that is + * not yet known to be connected. + * + * This happens quite often during testing when running mesh + * under valgrind: core connect notifications come very late and the + * DHT result has already come and created a valid path. + * In this case, the peer->connections hashmap will be NULL and + * tunnel_use_path will not be able to create a connection from that + * path. + * + * Re-running the DHT GET should give core time to callback. + */ + GNUNET_break(0); + rerun_dhtget = GNUNET_YES; + } + else + { + send_connection_create (c); + return; + } + } + } + + if (NULL != peer->dhtget && GNUNET_YES == rerun_dhtget) + { + GNUNET_DHT_get_stop (peer->dhtget); + peer->dhtget = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " Stopping DHT GET for peer %s\n", peer2s (peer)); + } + + if (NULL == peer->dhtget) + { + const struct GNUNET_PeerIdentity *id; + struct GNUNET_HashCode phash; + + id = GNUNET_PEER_resolve2 (peer->id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " Starting DHT GET for peer %s\n", peer2s (peer)); + GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &phash); + peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */ + GNUNET_BLOCK_TYPE_MESH_PEER, /* type */ + &phash, /* key to search */ + dht_replication_level, /* replication level */ + GNUNET_DHT_RO_RECORD_ROUTE | + GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, + NULL, /* xquery */ + 0, /* xquery bits */ + &dht_get_id_handler, peer); + if (MESH_TUNNEL_NEW == t->state) + tunnel_change_state (t, MESH_TUNNEL_SEARCHING); + } +} \ No newline at end of file -- 2.25.1