From 8d774a0d462c9de472d58594663c1dd8a850164b Mon Sep 17 00:00:00 2001 From: Bart Polot Date: Thu, 24 Oct 2013 02:04:48 +0000 Subject: - fix leaks - refactor to simplify APIs --- src/mesh/gnunet-service-mesh_connection.c | 57 ++++++++++--------- src/mesh/gnunet-service-mesh_connection.h | 4 +- src/mesh/gnunet-service-mesh_peer.c | 92 ++++++++++++++++--------------- src/mesh/gnunet-service-mesh_peer.h | 15 +++-- src/mesh/gnunet-service-mesh_tunnel.c | 15 +---- 5 files changed, 95 insertions(+), 88 deletions(-) diff --git a/src/mesh/gnunet-service-mesh_connection.c b/src/mesh/gnunet-service-mesh_connection.c index 2c07baa50..e856d7d71 100644 --- a/src/mesh/gnunet-service-mesh_connection.c +++ b/src/mesh/gnunet-service-mesh_connection.c @@ -35,7 +35,6 @@ #include "gnunet-service-mesh_connection.h" #include "gnunet-service-mesh_peer.h" #include "gnunet-service-mesh_tunnel.h" -#include "gnunet-service-mesh_channel.h" #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__) @@ -418,7 +417,7 @@ send_ack (struct MeshConnection *c, unsigned int buffer, int fwd) msg.ack = htonl (ack); msg.cid = c->id; - GMC_send_prebuilt_message (&msg.header, c, NULL, !fwd); + GMC_send_prebuilt_message (&msg.header, c, !fwd); } @@ -493,7 +492,7 @@ message_sent (void *cls, } p->idx = (p->idx + 1) % AVG_MSGS; -// if (NULL != c->t) +// if (NULL != c->t) FIXME // { // c->t->pending_messages--; // if (GNUNET_YES == c->t->destroy && 0 == t->pending_messages) @@ -517,6 +516,8 @@ get_prev_hop (const struct MeshConnection *c) { GNUNET_PEER_Id id; + LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos); + path_debug (c->path); if (0 == c->own_pos || c->path->length < 2) id = c->path->peers[0]; else @@ -596,7 +597,7 @@ is_fwd (const struct MeshConnection *c, * or a first CONNECTION_ACK directed to us. * * @param connection Connection to confirm. - * @param fwd Should we send it FWD? + * @param fwd Should we send it FWD? (root->dest) * (First (~SYNACK) goes BCK, second (~ACK) goes FWD) */ static void @@ -605,12 +606,12 @@ send_connection_ack (struct MeshConnection *connection, int fwd) struct MeshTunnel3 *t; t = connection->t; - LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n", + !fwd ? "FWD" : "BCK"); GMP_queue_add (get_hop (connection, fwd), NULL, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK, sizeof (struct GNUNET_MESH_ConnectionACK), - connection, NULL, fwd, - &message_sent, NULL); + connection, fwd, &message_sent, NULL); if (MESH_TUNNEL3_NEW == GMT_get_state (t)) GMT_change_state (t, MESH_TUNNEL3_WAITING); if (MESH_CONNECTION_READY != connection->state) @@ -639,7 +640,7 @@ send_broken (struct MeshConnection *c, msg.cid = c->id; msg.peer1 = *id1; msg.peer2 = *id2; - GMC_send_prebuilt_message (&msg.header, c, NULL, fwd); + GMC_send_prebuilt_message (&msg.header, c, fwd); } @@ -670,7 +671,7 @@ connection_keepalive (struct MeshConnection *c, int fwd) msg->header.type = htons (type); msg->cid = c->id; - GMC_send_prebuilt_message (&msg->header, c, NULL, fwd); + GMC_send_prebuilt_message (&msg->header, c, fwd); } @@ -846,7 +847,7 @@ connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL); msg.header.size = htons (sizeof (msg)); LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent); - GMC_send_prebuilt_message (&msg.header, c, NULL, fc == &c->fwd_fc); + GMC_send_prebuilt_message (&msg.header, c, fc == &c->fwd_fc); fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time); fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc); @@ -1141,7 +1142,7 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer, LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n"); GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO); GMP_add_path_to_origin (orig_peer, path, GNUNET_NO); - GMC_send_prebuilt_message (message, c, NULL, GNUNET_YES); + GMC_send_prebuilt_message (message, c, GNUNET_YES); } return GNUNET_OK; } @@ -1238,7 +1239,7 @@ GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer, } LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); - GMC_send_prebuilt_message (message, c, NULL, fwd); + GMC_send_prebuilt_message (message, c, fwd); return GNUNET_OK; } @@ -1287,7 +1288,7 @@ GMC_handle_broken (void* cls, } else { - GMC_send_prebuilt_message (message, c, NULL, fwd); + GMC_send_prebuilt_message (message, c, fwd); c->destroy = GNUNET_YES; } @@ -1338,7 +1339,7 @@ GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer, GNUNET_break_op (0); return GNUNET_OK; } - GMC_send_prebuilt_message (message, c, NULL, fwd); + GMC_send_prebuilt_message (message, c, fwd); c->destroy = GNUNET_YES; return GNUNET_OK; @@ -1467,7 +1468,7 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer, } GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO); - GMC_send_prebuilt_message (&msg->header, c, NULL, fwd); + GMC_send_prebuilt_message (&msg->header, c, fwd); return GNUNET_OK; } @@ -1689,7 +1690,7 @@ GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer, return GNUNET_OK; GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO); - GMC_send_prebuilt_message (message, c, NULL, fwd); + GMC_send_prebuilt_message (message, c, fwd); return GNUNET_OK; } @@ -1867,6 +1868,10 @@ GMC_destroy (struct MeshConnection *c) GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO); if (NULL != c->t) GMT_remove_connection (c->t, c); + + if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES)) + path_destroy (c->path); + GNUNET_free (c); } @@ -2021,14 +2026,19 @@ GMC_notify_broken (struct MeshConnection *c, GMC_destroy (c); return; } - send_broken (c, &my_full_id, GMP_get_id (peer), fwd); - connection_cancel_queues (c, !fwd); + if (GNUNET_NO == c->destroy) + send_broken (c, &my_full_id, GMP_get_id (peer), fwd); /* Connection will have at least one pending message * (the one we just scheduled), so no point in checking whether to * destroy immediately. */ c->destroy = GNUNET_YES; + /** + * Cancel all queues, if no message is left, connection will be destroyed. + */ + connection_cancel_queues (c, !fwd); + return; } @@ -2094,13 +2104,11 @@ GMC_is_sendable (struct MeshConnection *c, int fwd) * @param message Message to send. Function makes a copy of it. * If message is not hop-by-hop, decrements TTL of copy. * @param c Connection on which this message is transmitted. - * @param ch Channel on which this message is transmitted, or NULL. * @param fwd Is this a fwd message? */ void GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message, struct MeshConnection *c, - struct MeshChannel *ch, int fwd) { struct MeshFlowControl *fc; @@ -2199,7 +2207,7 @@ GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message, c->pending_messages++; - GMP_queue_add (get_hop (c, fwd), data, type, size, c, ch, fwd, + GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd, &message_sent, NULL); } @@ -2221,8 +2229,7 @@ GMC_send_create (struct MeshConnection *connection) LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n"); GMP_queue_add (get_next_hop (connection), NULL, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE, - size, connection, NULL, - GNUNET_YES, &message_sent, NULL); + size, connection, GNUNET_YES, &message_sent, NULL); state = GMT_get_state (connection->t); if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state) GMT_change_state (connection->t, MESH_TUNNEL3_WAITING); @@ -2256,9 +2263,9 @@ GMC_send_destroy (struct MeshConnection *c) GMC_2s (c)); if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES)) - GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES); + GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES); if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO)) - GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO); + GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO); c->destroy = GNUNET_YES; } diff --git a/src/mesh/gnunet-service-mesh_connection.h b/src/mesh/gnunet-service-mesh_connection.h index 3f75bcdab..fbff84335 100644 --- a/src/mesh/gnunet-service-mesh_connection.h +++ b/src/mesh/gnunet-service-mesh_connection.h @@ -223,6 +223,8 @@ GMC_shutdown (void); * @param t Tunnel this connection belongs to (or NULL); * @param p Path this connection has to use. * @param own_pos Own position in the @c p path. + * + * @return Newly created connection, NULL in case of error (own id not in path). */ struct MeshConnection * GMC_new (const struct GNUNET_HashCode *cid, @@ -398,13 +400,11 @@ GMC_is_sendable (struct MeshConnection *c, int fwd); * @param message Message to send. Function makes a copy of it. * If message is not hop-by-hop, decrements TTL of copy. * @param c Connection on which this message is transmitted. - * @param ch Channel on which this message is transmitted, or NULL. * @param fwd Is this a fwd message? */ void GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message, struct MeshConnection *c, - struct MeshChannel *ch, int fwd); /** diff --git a/src/mesh/gnunet-service-mesh_peer.c b/src/mesh/gnunet-service-mesh_peer.c index 38750f1ae..2ef116544 100644 --- a/src/mesh/gnunet-service-mesh_peer.c +++ b/src/mesh/gnunet-service-mesh_peer.c @@ -30,7 +30,6 @@ #include "gnunet-service-mesh_peer.h" #include "gnunet-service-mesh_dht.h" #include "gnunet-service-mesh_connection.h" -#include "gnunet-service-mesh_local.h" #include "gnunet-service-mesh_tunnel.h" #include "mesh_path.h" @@ -70,11 +69,6 @@ struct MeshPeerQueue */ int fwd; - /** - * Channel this message belongs to, if known. - */ - struct MeshChannel *ch; - /** * Pointer to info stucture used as cls. */ @@ -967,14 +961,13 @@ queue_send (void *cls, size_t size, void *buf) * @param type Type of the message, 0 for a raw message. * @param size Size of the message. * @param c Connection this message belongs to (cannot be NULL). - * @param ch Channel this message belongs to, if applicable (otherwise NULL). * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!) * @param cont Continuation to be called once CORE has taken the message. * @param cont_cls Closure for @c cont. */ void GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size, - struct MeshConnection *c, struct MeshChannel *ch, int fwd, + struct MeshConnection *c, int fwd, GMP_sent cont, void *cont_cls) { struct MeshPeerQueue *queue; @@ -982,8 +975,9 @@ GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size, int call_core; LOG (GNUNET_ERROR_TYPE_DEBUG, - "queue add %s %s (%u) on c %p, ch %p\n", - fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch); + "queue add %s %s towards %s (size %u) on c %p (%s)\n", + fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), GMP_2s(peer), + size, c, GMC_2s (c)); GNUNET_assert (NULL != c); if (NULL == peer->connections) @@ -1010,7 +1004,6 @@ GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size, queue->size = size; queue->peer = peer; queue->c = c; - queue->ch = ch; queue->fwd = fwd; queue->callback = cont; queue->callback_cls = cont_cls; @@ -1447,36 +1440,42 @@ GMP_add_connection (struct MeshPeer *peer, * Add the path to the peer and update the path used to reach it in case this * is the shortest. * - * @param peer_info Destination peer to add the path to. + * @param peer Destination peer to add the path to. * @param path New path to add. Last peer must be the peer in arg 1. * Path will be either used of freed if already known. * @param trusted Do we trust that this path is real? + * + * @return path if path was taken, pointer to existing duplicate if exists + * NULL on error. */ -void -GMP_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path, +struct MeshPeerPath * +GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *path, int trusted) { struct MeshPeerPath *aux; unsigned int l; unsigned int l2; - if ((NULL == peer_info) || (NULL == path)) + LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n", + path->length, GMP_2s (peer)); + + if ((NULL == peer) || (NULL == path)) { GNUNET_break (0); path_destroy (path); - return; + return NULL; } - if (path->peers[path->length - 1] != peer_info->id) + if (path->peers[path->length - 1] != peer->id) { GNUNET_break (0); path_destroy (path); - return; + return NULL; } if (2 >= path->length && GNUNET_NO == trusted) { /* Only allow CORE to tell us about direct paths */ path_destroy (path); - return; + return NULL; } for (l = 1; l < path->length; l++) { @@ -1487,45 +1486,47 @@ GMP_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path, { path->peers[l2] = path->peers[l + l2]; } - path->length -= l; - l = 1; - path->peers = - GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id)); + path->length -= l; + l = 1; + path->peers = GNUNET_realloc (path->peers, + path->length * sizeof (GNUNET_PEER_Id)); } } - LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n", - path->length, GMP_2s (peer_info)); + LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u]\n", path->length); l = path_get_length (path); if (0 == l) { path_destroy (path); - return; + return NULL; } - GNUNET_assert (peer_info->id == path->peers[path->length - 1]); - for (aux = peer_info->path_head; aux != NULL; aux = aux->next) + GNUNET_assert (peer->id == path->peers[path->length - 1]); + for (aux = peer->path_head; aux != NULL; aux = aux->next) { l2 = path_get_length (aux); if (l2 > l) { - GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head, - peer_info->path_tail, aux, path); - return; + LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n"); + GNUNET_CONTAINER_DLL_insert_before (peer->path_head, + peer->path_tail, aux, path); + return path; + } + else + { + if (l2 == l && memcmp (path->peers, aux->peers, l) == 0) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n"); + path_destroy (path); + return aux; + } } - else - { - if (l2 == l && memcmp (path->peers, aux->peers, l) == 0) - { - path_destroy (path); - return; - } - } } - GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail, + GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail, path); - return; + LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n"); + return path; } @@ -1539,16 +1540,19 @@ GMP_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path, * @param path New path to add after being inversed. * Path will be either used or freed. * @param trusted Do we trust that this path is real? + * + * @return path if path was taken, pointer to existing duplicate if exists + * NULL on error. */ -void +struct MeshPeerPath * GMP_add_path_to_origin (struct MeshPeer *peer, struct MeshPeerPath *path, int trusted) { if (NULL == path) - return; + return NULL; path_invert (path); - GMP_add_path (peer, path, trusted); + return GMP_add_path (peer, path, trusted); } diff --git a/src/mesh/gnunet-service-mesh_peer.h b/src/mesh/gnunet-service-mesh_peer.h index df044ad14..f3082543f 100644 --- a/src/mesh/gnunet-service-mesh_peer.h +++ b/src/mesh/gnunet-service-mesh_peer.h @@ -123,14 +123,13 @@ GMP_connect (struct MeshPeer *peer); * @param type Type of the message, 0 for a raw message. * @param size Size of the message. * @param c Connection this message belongs to (cannot be NULL). - * @param ch Channel this message belongs to, if applicable (otherwise NULL). * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!) * @param cont Continuation to be called once CORE has taken the message. * @param cont_cls Closure for @c cont. */ void GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size, - struct MeshConnection *c, struct MeshChannel *ch, int fwd, + struct MeshConnection *c, int fwd, GMP_sent cont, void *cont_cls); /** @@ -194,12 +193,15 @@ GMP_add_connection (struct MeshPeer *peer, struct MeshConnection *c); * Add the path to the peer and update the path used to reach it in case this * is the shortest. * - * @param peer_info Destination peer to add the path to. + * @param peer Destination peer to add the path to. * @param path New path to add. Last peer must be the peer in arg 1. * Path will be either used of freed if already known. * @param trusted Do we trust that this path is real? + * + * @return path if path was taken, pointer to existing duplicate if exists + * NULL on error. */ -void +struct MeshPeerPath * GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *p, int trusted); /** @@ -212,8 +214,11 @@ GMP_add_path (struct MeshPeer *peer, struct MeshPeerPath *p, int trusted); * @param path New path to add after being inversed. * Path will be either used or freed. * @param trusted Do we trust that this path is real? + * + * @return path if path was taken, pointer to existing duplicate if exists + * NULL on error. */ -void +struct MeshPeerPath * GMP_add_path_to_origin (struct MeshPeer *peer, struct MeshPeerPath *path, int trusted); diff --git a/src/mesh/gnunet-service-mesh_tunnel.c b/src/mesh/gnunet-service-mesh_tunnel.c index 41e7deb4e..6cb536110 100644 --- a/src/mesh/gnunet-service-mesh_tunnel.c +++ b/src/mesh/gnunet-service-mesh_tunnel.c @@ -117,11 +117,6 @@ struct MeshTunnel3 */ MESH_ChannelNumber next_chid; - /** - * Pending message count. - */ - int pending_messages; - /** * Destroy flag: if true, destroy on last message. */ @@ -848,10 +843,7 @@ GMT_destroy_empty (struct MeshTunnel3 *t) GMC_send_destroy (iter->c); } - if (0 == t->pending_messages) - GMT_destroy (t); - else - t->destroy = GNUNET_YES; + t->destroy = GNUNET_YES; } @@ -1249,7 +1241,7 @@ GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message, uint64_t iv; uint16_t type; - LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMP_2s (t->peer)); + LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMT_2s (t)); iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX); msg = (struct GNUNET_MESH_Encrypted *) cbuf; @@ -1280,8 +1272,7 @@ GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message, } msg->reserved = 0; - t->pending_messages++; - GMC_send_prebuilt_message (&msg->header, c, ch, fwd); + GMC_send_prebuilt_message (&msg->header, c, fwd); } /** -- cgit v1.2.3