Small adjustments for secio protocol compatibility

yamux
John Jones 2017-08-31 12:49:43 -05:00
parent 7829ab0aee
commit 36061dc1da
5 changed files with 50 additions and 30 deletions

View File

@ -34,14 +34,7 @@ int libp2p_net_multistream_close(void* stream_context) {
struct Stream* stream = secure_context->default_stream;
if (stream == NULL || stream->socket_descriptor == NULL)
return 1;
int socket_descriptor = *((int*)stream->socket_descriptor);
close(socket_descriptor);
free(stream->socket_descriptor);
stream->socket_descriptor = NULL;
if (stream->address != NULL)
multiaddress_free(stream->address);
stream->address = NULL;
free(stream);
libp2p_net_multistream_stream_free(stream);
secure_context->default_stream = NULL;
secure_context->insecure_stream = NULL;
secure_context->secure_stream = NULL;

View File

@ -157,7 +157,7 @@ int libp2p_peerstore_add_peer(struct Peerstore* peerstore, const struct Libp2pPe
* @returns the PeerEntry struct if found, otherwise NULL
*/
struct PeerEntry* libp2p_peerstore_get_peer_entry(struct Peerstore* peerstore, const unsigned char* peer_id, size_t peer_id_size) {
if (peer_id_size == 0 || peer_id == NULL)
if (peer_id_size == 0 || peer_id == NULL || peerstore == NULL)
return NULL;
struct Libp2pLinkedList* current = peerstore->head_entry;

View File

@ -42,7 +42,7 @@ int libp2p_secio_can_handle(const uint8_t* incoming, size_t incoming_size) {
// sanity checks
if (incoming_size < 11)
return 0;
char* result = strstr((char*)incoming, "/ipfs/secio");
char* result = strstr((char*)incoming, "/secio/1.0.0");
if (result != NULL && result == (char*)incoming)
return 1;
return 0;
@ -576,8 +576,7 @@ int libp2p_secio_unencrypted_read(struct SessionContext* session, unsigned char*
int read_this_time = 0;
do {
read_this_time = socket_read(*((int*)session->insecure_stream->socket_descriptor), &size[read], 1, 0, timeout_secs);
if (read_this_time < 0) {
read_this_time = 0;
if (read_this_time <= 0) {
if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
// TODO: use epoll or select to wait for socket to be writable
libp2p_logger_debug("secio", "Attempted read, but got EAGAIN or EWOULDBLOCK. Code %d.\n", errno);
@ -586,9 +585,6 @@ int libp2p_secio_unencrypted_read(struct SessionContext* session, unsigned char*
libp2p_logger_error("secio", "Error in libp2p_secio_unencrypted_read: %s\n", strerror(errno));
return 0;
}
}
if (read == 0 && size[0] == 10) {
libp2p_logger_error("secio", "Spurrious newline found.\n");
} else {
left = left - read_this_time;
read += read_this_time;
@ -605,6 +601,10 @@ int libp2p_secio_unencrypted_read(struct SessionContext* session, unsigned char*
read = 0;
read_this_time = 0;
*results = malloc(left);
if (*results == NULL) {
libp2p_logger_error("secio", "Unable to allocate memory for the incoming message. Size: %ulld", left);
return 0;
}
unsigned char* ptr = *results;
do {
read_this_time = socket_read(*((int*)session->insecure_stream->socket_descriptor), (char*)&ptr[read], left, 0, timeout_secs);
@ -616,6 +616,10 @@ int libp2p_secio_unencrypted_read(struct SessionContext* session, unsigned char*
libp2p_logger_error("secio", "read from socket returned %d.\n", errno);
return 0;
}
} else if (read_this_time == 0) {
// socket_read returned 0, which it shouldn't
libp2p_logger_error("secio", "socket_read returned 0 trying to read from %s.\n", session->remote_peer_id);
return 0;
}
left = left - read_this_time;
} while (left > 0);
@ -809,7 +813,7 @@ int libp2p_secio_encrypted_read(void* stream_context, unsigned char** bytes, siz
* NOTE: session must contain a valid socket_descriptor that is a multistream.
* @param local_session the secure session to be filled
* @param private_key our private key to use
* @param remote_requested it is the other side that requested the upgrade to secio
* @param peerstore the collection of peers
* @returns true(1) on success, false(0) otherwise
*/
int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPrivateKey* private_key, struct Peerstore* peerstore) {
@ -884,8 +888,9 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva
if (libp2p_secio_propose_protobuf_encode(propose_out, propose_out_bytes, propose_out_size, &propose_out_size) == 0)
goto exit;
// now send the Propose struct
// now send the protocol and Propose struct
bytes_written = libp2p_secio_unencrypted_write(local_session, propose_out_bytes, propose_out_size);
if (bytes_written != propose_out_size) {
libp2p_logger_error("secio", "Sent propose_out, but did not write the correct number of bytes. Should be %d but was %d.\n", propose_out_size, bytes_written);
}
@ -897,7 +902,7 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva
goto exit;
}
if (!libp2p_secio_propose_protobuf_decode(propose_in_bytes, propose_in_size, &propose_in)) {
if (!libp2p_secio_propose_protobuf_decode(propose_in_bytes, propose_in_size -1, &propose_in)) {
libp2p_logger_error("secio", "Unable to un-protobuf the remote's Propose struct\n");
goto exit;
}

View File

@ -14,6 +14,10 @@
void print_stretched_key(struct StretchedKey* key) {
fprintf(stdout, "cipher key: ");
if (key == NULL) {
fprintf(stdout, "NULL\n");
return;
}
for(int i = 0; i < key->cipher_size; i++) {
fprintf(stdout, "%d ", key->cipher_key[i]);
}
@ -46,6 +50,8 @@ int test_secio_handshake() {
struct PrivateKey* private_key = NULL;
struct SessionContext secure_session = {0};
struct Peerstore *peerstore = NULL;
struct Libp2pPeer* local_peer = NULL;
// 1) take the private key and turn it back into bytes (decode base 64)
decode_base64_size = libp2p_crypto_encoding_base64_decode_size(strlen(orig_priv_key));
@ -68,8 +74,10 @@ int test_secio_handshake() {
if (!libp2p_crypto_rsa_private_key_fill_public_key(rsa_private_key))
goto exit;
local_peer = libp2p_peer_new();
peerstore = libp2p_peerstore_new(local_peer);
//secure_session.host = "www.jmjatlanta.com";
secure_session.host = "10.211.55.4";
secure_session.host = "10.211.55.2";
secure_session.port = 4001;
secure_session.traffic_type = TCP;
// connect to host
@ -80,16 +88,28 @@ int test_secio_handshake() {
goto exit;
}
if (!libp2p_secio_handshake(&secure_session, rsa_private_key, NULL)) {
// attempt to write the protocol, and see what comes back
char* protocol = "/secio/1.0.0\n";
int protocol_size = strlen(protocol);
secure_session.insecure_stream->write(&secure_session, protocol, protocol_size);
unsigned char* buffer = NULL;
size_t bytes_read = 0;
int timeout = 30;
secure_session.insecure_stream->read(&secure_session, &buffer, &bytes_read, timeout);
if (!libp2p_secio_handshake(&secure_session, rsa_private_key, peerstore)) {
fprintf(stderr, "test_secio_handshake: Unable to do handshake\n");
fprintf(stdout, "Shared key: ");
for(int i = 0; i < secure_session.shared_key_size; i++)
fprintf(stdout, "%d ", secure_session.shared_key[i]);
fprintf(stdout, "\nLocal stretched key: ");
print_stretched_key(secure_session.local_stretched_key);
fprintf(stdout, "\nRemote stretched key: ");
print_stretched_key(secure_session.remote_stretched_key);
fprintf(stdout, "\n");
if (secure_session.shared_key != NULL) {
fprintf(stdout, "Shared key: ");
for(int i = 0; i < secure_session.shared_key_size; i++)
fprintf(stdout, "%d ", secure_session.shared_key[i]);
fprintf(stdout, "\nLocal stretched key: ");
print_stretched_key(secure_session.local_stretched_key);
fprintf(stdout, "\nRemote stretched key: ");
print_stretched_key(secure_session.remote_stretched_key);
fprintf(stdout, "\n");
}
goto exit;
}
@ -156,6 +176,8 @@ int test_secio_handshake() {
free(decode_base64);
if (rsa_private_key != NULL)
libp2p_crypto_rsa_rsa_private_key_free(rsa_private_key);
if (peerstore != NULL)
libp2p_peerstore_free(peerstore);
return retVal;
}

View File

@ -98,7 +98,7 @@ void libp2p_logger_log(const char* area, int log_level, const char* format, ...)
libp2p_logger_init();
if (log_level <= CURRENT_LOGLEVEL) {
if (libp2p_logger_watching_class(area)) {
int new_format_size = strlen(format) + strlen(area) + 10;
int new_format_size = strlen(format) + strlen(area) + strlen(libp2p_logger_log_level_to_string(log_level)) + 10;
char new_format[new_format_size];
sprintf(&new_format[0], "[%s][%s] %s", libp2p_logger_log_level_to_string(log_level), area, format);
va_list argptr;
@ -128,7 +128,7 @@ void libp2p_logger_vlog(const char* area, int log_level, const char* format, va_
else
found = libp2p_logger_watching_class(area);
if (found) {
int new_format_size = strlen(format) + strlen(area) + 10;
int new_format_size = strlen(format) + strlen(area) + strlen(libp2p_logger_log_level_to_string(log_level)) + 10;
char new_format[new_format_size];
sprintf(&new_format[0], "[%s][%s] %s", libp2p_logger_log_level_to_string(log_level), area, format);
vfprintf(stderr, new_format, argptr);