Finishing NodeIO

This commit is contained in:
John Jones 2017-03-19 14:40:16 -05:00
parent 0b238eb5ac
commit cfcabaecd0
8 changed files with 143 additions and 52 deletions

View file

@ -11,6 +11,9 @@
#include "ipfs/routing/routing.h"
#include "ipfs/core/ipfs_node.h"
#include "libp2p/secio/secio.h"
#include "libp2p/nodeio/nodeio.h"
#include "ipfs/merkledag/merkledag.h"
#include "ipfs/merkledag/node.h"
#define BUF_SIZE 4096
@ -19,6 +22,11 @@ int ipfs_null_requesting_secio(unsigned char* buffer, size_t buffer_size) {
return 1;
return 0;
}
int ipfs_null_requesting_nodeio(unsigned char* buffer, size_t buffer_size) {
if (buffer_size > 5 && strncmp((char*)buffer, "/nodeio", 7) == 0)
return 1;
return 0;
}
/**
* We've received a connection. Find out what they want
*/
@ -32,26 +40,57 @@ void *ipfs_null_connection (void *ptr)
// TODO: multistream + secio + message.
// TODO: when should we exit the for loop and disconnect?
struct SessionContext secure_session;
secure_session.insecure_stream = libp2p_net_multistream_stream_new(connection_param->socket);
secure_session.default_stream = secure_session.insecure_stream;
struct SessionContext session;
session.insecure_stream = libp2p_net_multistream_stream_new(connection_param->socket);
session.default_stream = session.insecure_stream;
fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count));
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);
if (libp2p_net_multistream_negotiate(session.insecure_stream)) {
routing = ipfs_routing_new_online(connection_param->local_node, &connection_param->local_node->identity->private_key, session.insecure_stream);
for(;;) {
// check if they're looking for an upgrade (i.e. secio)
unsigned char* results = NULL;
size_t bytes_read;
secure_session.default_stream->read(&secure_session, &results, &bytes_read);
session.default_stream->read(&session, &results, &bytes_read);
if (ipfs_null_requesting_secio(results, bytes_read)) {
if (!libp2p_secio_handshake(&secure_session, &connection_param->local_node->identity->private_key, 1)) {
if (!libp2p_secio_handshake(&session, &connection_param->local_node->identity->private_key, 1)) {
// rejecting connection
break;
}
} else {
} else if (ipfs_null_requesting_nodeio(results, bytes_read)) {
if (!libp2p_nodeio_handshake(&session))
break;
// loop through file requests
int _continue = 1;
while(_continue) {
unsigned char* hash;
size_t hash_length = 0;
_continue = session.default_stream->read(&session, &hash, &hash_length);
if (hash_length < 20) {
_continue = 0;
continue;
}
else {
// try to get the Node
struct Node* node = NULL;
if (!ipfs_merkledag_get(hash, hash_length, &node, connection_param->local_node->repo)) {
_continue = 0;
continue;
}
size_t results_size = ipfs_node_protobuf_encode_size(node);
unsigned char results[results_size];
if (!ipfs_node_protobuf_encode(node, results, results_size, &results_size)) {
_continue = 0;
continue;
}
// send it to the requestor
session.default_stream->write(&session, results, results_size);
}
}
}
else {
struct Libp2pMessage* msg = NULL;
libp2p_message_protobuf_decode(results, bytes_read, &msg);
if (msg != NULL) {
@ -65,9 +104,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.
secure_session.default_stream->write(&secure_session, 0, 1);
session.default_stream->write(&session, 0, 1);
} else {
secure_session.default_stream->write(&secure_session, val, val_size);
session.default_stream->write(&session, val, val_size);
}
break;
}
@ -94,8 +133,8 @@ void *ipfs_null_connection (void *ptr)
}
*/
if (secure_session.default_stream != NULL) {
secure_session.default_stream->close(&secure_session);
if (session.default_stream != NULL) {
session.default_stream->close(&session);
}
(*(connection_param->count))--; // update counter.
free (connection_param);