Working on streams

yamux
John Jones 2017-03-09 12:49:47 -05:00
parent 1fa0d4bd68
commit 74d95cb696
3 changed files with 18 additions and 6 deletions

View File

@ -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) {

View File

@ -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))

View File

@ -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;