From 4542ebe1cb13993a92af7bd7dc3c88893cd26cce Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 31 Jul 2017 08:38:16 -0500 Subject: [PATCH] correctly handle a stream that is unable to connect --- net/multistream.c | 5 +++-- peer/peer.c | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/net/multistream.c b/net/multistream.c index af470df..6dd6c49 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -64,14 +64,15 @@ int libp2p_net_multistream_write(void* stream_context, const unsigned char* data memcpy(&buffer[varint_size], data, data_length); // determine if this should run through the secio protocol or not if (session_context->secure_stream == NULL) { + int sd = *((int*)stream->socket_descriptor); // do a "raw" write - num_bytes = socket_write(*((int*)stream->socket_descriptor), (char*)varint, varint_size, 0); + num_bytes = socket_write(sd, (char*)varint, varint_size, 0); if (num_bytes == 0) { free(buffer); return 0; } // then send the actual data - num_bytes += socket_write(*((int*)stream->socket_descriptor), (char*)data, data_length, 0); + num_bytes += socket_write(sd, (char*)data, data_length, 0); } else { // write using secio num_bytes = stream->write(stream_context, buffer, data_length + varint_size); diff --git a/peer/peer.c b/peer/peer.c index 8414a57..c7f64db 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -93,6 +93,10 @@ int libp2p_peer_connect(struct RsaPrivateKey* privateKey, struct Libp2pPeer* pee int port = multiaddress_get_ip_port(ma); peer->sessionContext = libp2p_session_context_new(); peer->sessionContext->insecure_stream = libp2p_net_multistream_connect(ip, port); + if (peer->sessionContext->insecure_stream == NULL) { + free(ip); + return 0; + } if (peer->sessionContext->insecure_stream != NULL) { peer->sessionContext->default_stream = peer->sessionContext->insecure_stream; peer->connection_type = CONNECTION_TYPE_CONNECTED;