From 1971e60438d44453f052582723e4afd3d03e083a Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 27 Jul 2017 12:06:27 -0500 Subject: [PATCH] Cleanup of needless Stream attached to things it shouldn't have --- include/libp2p/conn/session.h | 12 ++++++++++++ include/libp2p/peer/peer.h | 2 +- net/multistream.c | 2 +- peer/peer.c | 22 ++++++++++++++-------- secio/secio.c | 4 ++-- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/include/libp2p/conn/session.h b/include/libp2p/conn/session.h index 8ddd16a..208deff 100644 --- a/include/libp2p/conn/session.h +++ b/include/libp2p/conn/session.h @@ -63,6 +63,18 @@ struct SessionContext { size_t remote_ephemeral_public_key_size; }; +/*** + * Allocate resources for a new SessionContext struct + * @returns the newly allocated SessionContext, or NULL + */ +struct SessionContext* libp2p_session_context_new(); +/** + * Free resources of a SessionContext struct + * @param context the SessionContext + * @returns true(1) + */ +int libp2p_session_context_free(struct SessionContext* session); + /*** * Compare 2 SessionContext structs for equality * @param a side A diff --git a/include/libp2p/peer/peer.h b/include/libp2p/peer/peer.h index 173b58c..8ba95d0 100644 --- a/include/libp2p/peer/peer.h +++ b/include/libp2p/peer/peer.h @@ -19,7 +19,7 @@ struct Libp2pPeer { size_t id_size; // the length of id struct Libp2pLinkedList* addr_head; // protobuf field 2 of multiaddr bytes (repeatable) (stored here as a struct MultiAddr) enum ConnectionType connection_type; // protobuf field 3 (a varint) - struct Stream *connection; // not protobuf'd, the current connection to the peer + struct SessionContext *sessionContext; // not protobuf'd, the current connection to the peer }; /** diff --git a/net/multistream.c b/net/multistream.c index 33a3ac3..5ce5a1e 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -30,7 +30,7 @@ int libp2p_net_multistream_close(void* stream_context) { /** * Write to an open multistream host - * @param socket_fd the socket file descriptor + * @param stream_context the session context * @param data the data to send * @param data_length the length of the data * @returns the number of bytes written diff --git a/peer/peer.c b/peer/peer.c index c536916..89de6fc 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -19,7 +19,7 @@ struct Libp2pPeer* libp2p_peer_new() { out->id_size = 0; out->addr_head = NULL; out->connection_type = CONNECTION_TYPE_NOT_CONNECTED; - out->connection = NULL; + out->sessionContext = NULL; } return out; } @@ -60,8 +60,10 @@ int libp2p_peer_connect(struct Libp2pPeer* peer, int timeout) { if (!multiaddress_get_ip_address(ma, &ip)) continue; int port = multiaddress_get_ip_port(ma); - peer->connection = libp2p_net_multistream_connect(ip, port); - if (peer->connection != NULL) { + peer->sessionContext = libp2p_session_context_new(); + peer->sessionContext->insecure_stream = libp2p_net_multistream_connect(ip, port); + if (peer->sessionContext->insecure_stream != NULL) { + peer->sessionContext->default_stream = peer->sessionContext->insecure_stream; peer->connection_type = CONNECTION_TYPE_CONNECTED; } free(ip); @@ -112,9 +114,10 @@ void libp2p_peer_free(struct Libp2pPeer* in) { } if (in->id != NULL) free(in->id); - if (in->connection != NULL) { - libp2p_net_multistream_stream_free(in->connection); - in->connection = NULL; + if (in->sessionContext != NULL) { + libp2p_session_context_free(in->sessionContext); + //libp2p_net_multistream_stream_free(in->connection); + in->sessionContext = NULL; } // free the memory in the linked list struct Libp2pLinkedList* current = in->addr_head; @@ -130,6 +133,9 @@ void libp2p_peer_free(struct Libp2pPeer* in) { /** * Make a copy of a peer + * + * NOTE: SessionContext is not copied. + * * @param in what is to be copied * @returns a new struct, that does not rely on the old */ @@ -159,7 +165,7 @@ struct Libp2pPeer* libp2p_peer_copy(const struct Libp2pPeer* in) { current_out = copy_item; current_in = current_in->next; } - out->connection = in->connection; + out->sessionContext = in->sessionContext; } return out; } @@ -185,7 +191,7 @@ int libp2p_peer_matches_id(struct Libp2pPeer* in, const unsigned char* peer_id) */ int libp2p_peer_is_connected(struct Libp2pPeer* in) { if (in->connection_type == CONNECTION_TYPE_CONNECTED) { - if (in->connection == NULL) { + if (in->sessionContext == NULL || in->sessionContext->default_stream == NULL) { in->connection_type = CONNECTION_TYPE_NOT_CONNECTED; } } diff --git a/secio/secio.c b/secio/secio.c index 03985ed..5f9f500 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -478,7 +478,7 @@ int libp2p_secio_unencrypted_write(struct SessionContext* session, unsigned char int written = 0; int written_this_time = 0; do { - written_this_time = socket_write(*((int*)session->insecure_stream->socket_descriptor), &size_as_char[written], left, 0); + written_this_time = socket_write(*((int*)session->default_stream->socket_descriptor), &size_as_char[written], left, 0); if (written_this_time < 0) { written_this_time = 0; if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) { @@ -493,7 +493,7 @@ int libp2p_secio_unencrypted_write(struct SessionContext* session, unsigned char left = data_length; written = 0; do { - written_this_time = socket_write(*((int*)session->insecure_stream->socket_descriptor), (char*)&bytes[written], left, 0); + written_this_time = socket_write(*((int*)session->default_stream->socket_descriptor), (char*)&bytes[written], left, 0); if (written_this_time < 0) { written_this_time = 0; if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) {