From 15a8abff9a7abd417b90a4ebda39ecfa9fac27ad Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 9 Mar 2017 10:01:09 -0500 Subject: [PATCH] Made the stream methods more generic --- core/null.c | 23 +++++++++++------------ test/core/test_ping.h | 4 +++- test/node/test_resolver.h | 3 ++- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/core/null.c b/core/null.c index 8888bd1..c1a86ea 100644 --- a/core/null.c +++ b/core/null.c @@ -26,28 +26,27 @@ void *ipfs_null_connection (void *ptr) { struct null_connection_params *connection_param = NULL; struct s_ipfs_routing* routing = NULL; - struct Stream* stream = NULL; connection_param = (struct null_connection_params*) ptr; // TODO: multistream + secio + message. // TODO: when should we exit the for loop and disconnect? - stream = libp2p_net_multistream_stream_new(connection_param->socket); + struct SecureSession secure_session; + secure_session.insecure_stream = libp2p_net_multistream_stream_new(connection_param->socket); + secure_session.default_stream = secure_session.insecure_stream; fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count)); - if (libp2p_net_multistream_negotiate(stream)) { - routing = ipfs_routing_new_online(connection_param->local_node, &connection_param->local_node->identity->private_key, stream); + if (libp2p_net_multistream_negotiate(secure_session.insecure_stream)) { + routing = ipfs_routing_new_online(connection_param->local_node, &connection_param->local_node->identity->private_key, secure_session.insecure_stream); for(;;) { // check if they're looking for an upgrade (i.e. secio) unsigned char* results = NULL; size_t bytes_read; - libp2p_net_multistream_read(stream, &results, &bytes_read); - if (ipfs_null_requesting_secio(results, bytes_read)) { - struct SecureSession secure_session; - secure_session.stream = stream; + secure_session.default_stream->read(&secure_session, &results, &bytes_read); + if (secure_session.secure_stream == NULL && ipfs_null_requesting_secio(results, bytes_read)) { if (!libp2p_secio_handshake(&secure_session, &connection_param->local_node->identity->private_key, 1)) { // rejecting connection break; @@ -66,9 +65,9 @@ void *ipfs_null_connection (void *ptr) routing->GetValue(routing, msg->key, msg->key_size, (void**)&val, &val_size); if (val == NULL) { // write a 0 to the stream to tell the client we couldn't find it. - stream->write(stream, 0, 1); + secure_session.default_stream->write(&secure_session, 0, 1); } else { - stream->write(stream, val, val_size); + secure_session.default_stream->write(&secure_session, val, val_size); } break; } @@ -95,8 +94,8 @@ void *ipfs_null_connection (void *ptr) } */ - if (stream != NULL) { - stream->close(stream); + if (secure_session.default_stream != NULL) { + secure_session.default_stream->close(&secure_session); } (*(connection_param->count))--; // update counter. free (connection_param); diff --git a/test/core/test_ping.h b/test/core/test_ping.h index 3eeba91..5ef0af3 100644 --- a/test/core/test_ping.h +++ b/test/core/test_ping.h @@ -19,7 +19,7 @@ int test_ping() { int retVal = 0; struct FSRepo* fs_repo = NULL; struct Libp2pMessage* message = NULL; - struct IpfsNode local_node; + //struct IpfsNode local_node; struct Libp2pPeer* remote_peer = NULL; struct Dialer* dialer = NULL; struct Connection* conn = NULL; @@ -32,10 +32,12 @@ int test_ping() { drop_build_and_open_repo("/tmp/.ipfs", &fs_repo); // create a new IpfsNode + /* local_node.mode = MODE_ONLINE; local_node.peerstore = libp2p_peerstore_new(); local_node.repo = fs_repo; local_node.identity = fs_repo->config->identity; + */ // build the ping message message = libp2p_message_new(); diff --git a/test/node/test_resolver.h b/test/node/test_resolver.h index be63135..ced344d 100644 --- a/test/node/test_resolver.h +++ b/test/node/test_resolver.h @@ -87,7 +87,8 @@ int test_resolver_remote_get() { // start the daemon in a separate thread pthread_t thread; - int rc = pthread_create(&thread, NULL, test_resolver_daemon_start, (void*)ipfs_path); + if (pthread_create(&thread, NULL, test_resolver_daemon_start, (void*)ipfs_path) < 0) + return 0; // this should point to a test directory with files and directories char* home_dir = os_utils_get_homedir();