From 74d95cb696b6da141e6b751faa8abb5495d5ee45 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 9 Mar 2017 12:49:47 -0500 Subject: [PATCH] Working on streams --- net/multistream.c | 18 ++++++++++++++---- record/message.c | 2 ++ secio/secio.c | 4 ++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/net/multistream.c b/net/multistream.c index 300ead2..1b96f46 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -135,12 +135,16 @@ struct Stream* libp2p_net_multistream_connect(const char* hostname, int port) { if (stream == NULL) goto exit; + struct SecureSession session; + session.insecure_stream = stream; + session.default_stream = stream; + // try to receive the protocol id - return_result = libp2p_net_multistream_read(stream, &results, &results_size); + return_result = libp2p_net_multistream_read(&session, &results, &results_size); if (return_result == 0 || results_size < 1) goto exit; - num_bytes = libp2p_net_multistream_write(stream, (unsigned char*)protocol_buffer, strlen(protocol_buffer)); + num_bytes = libp2p_net_multistream_write(&session, (unsigned char*)protocol_buffer, strlen(protocol_buffer)); if (num_bytes <= 0) goto exit; @@ -166,10 +170,13 @@ int libp2p_net_multistream_negotiate(struct Stream* stream) { size_t results_length = 0; int retVal = 0; // send the protocol id - if (!libp2p_net_multistream_write(stream, (unsigned char*)protocolID, strlen(protocolID))) + struct SecureSession secure_session; + secure_session.insecure_stream = stream; + secure_session.default_stream = stream; + if (!libp2p_net_multistream_write(&secure_session, (unsigned char*)protocolID, strlen(protocolID))) goto exit; // expect the same back - libp2p_net_multistream_read(stream, &results, &results_length); + libp2p_net_multistream_read(&secure_session, &results, &results_length); if (results_length == 0) goto exit; if (strncmp((char*)results, protocolID, strlen(protocolID)) != 0) @@ -181,11 +188,13 @@ int libp2p_net_multistream_negotiate(struct Stream* stream) { return retVal; } + /** * Expect to read a message * @param fd the socket file descriptor * @returns the retrieved message, or NULL */ +/* struct Libp2pMessage* libp2p_net_multistream_get_message(struct Stream* stream) { int retVal = 0; unsigned char* results = NULL; @@ -206,6 +215,7 @@ struct Libp2pMessage* libp2p_net_multistream_get_message(struct Stream* stream) return msg; } +*/ void libp2p_net_multistream_stream_free(struct Stream* stream) { if (stream != NULL) { diff --git a/record/message.c b/record/message.c index 8276675..e8223c0 100644 --- a/record/message.c +++ b/record/message.c @@ -182,6 +182,8 @@ int libp2p_message_protobuf_decode(unsigned char* in, size_t in_size, struct Lib goto exit; } pos += bytes_read; + if (field_no < 1 || field_no > 10) + goto exit; switch(field_no) { case (1): // message type if (!protobuf_decode_varint(&in[pos], in_size - pos, (long long unsigned int*)&ptr->message_type , &bytes_read)) diff --git a/secio/secio.c b/secio/secio.c index 81c7d87..433d728 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -679,14 +679,14 @@ int libp2p_secio_handshake(struct SecureSession* local_session, struct RsaPrivat memcpy(total, protocol, protocol_len); memcpy(&total[protocol_len], propose_out_bytes, propose_out_size); - bytes_written = libp2p_net_multistream_write(local_session->insecure_stream, total, protocol_len + propose_out_size); + bytes_written = libp2p_net_multistream_write(local_session, total, protocol_len + propose_out_size); free(total); if (bytes_written <= 0) goto exit; if (!remote_requested) { // we should get back the secio confirmation - bytes_written = libp2p_net_multistream_read(local_session->insecure_stream, &results, &results_size); + bytes_written = libp2p_net_multistream_read(local_session, &results, &results_size); if (bytes_written < 5 || strstr((char*)results, "secio") == NULL) goto exit;