Fixes for connections via secio

This commit is contained in:
John Jones 2017-03-02 16:18:02 -05:00
parent 3a38623dcc
commit d4fee344a7
3 changed files with 49 additions and 43 deletions

View file

@ -26,12 +26,17 @@ int ipfs_core_net_listen(struct IpfsNode* node, char* protocol, struct IpfsListe
/***
* Dial a peer
* @param node this node
* @param peer_id who to dial
* @param peer_id who to dial (null terminated string)
* @param protocol the protocol to use
* @param stream the resultant stream
* @returns true(1) on success, otherwise false(0)
*/
int ipsf_core_net_dial(struct IpfsNode* node, char* peer_id, char* protocol, struct Stream* stream) {
int ipsf_core_net_dial(const struct IpfsNode* node, const char* peer_id, const char* protocol, struct Stream* stream) {
//TODO: Implement this
// get the multiaddress from the peerstore
struct Libp2pPeer* peer = libp2p_peerstore_get_peer(node->peerstore, peer_id, strlen(peer_id));
// attempt to connect
// attempt to use the protocol passed in
return 0;
}

View file

@ -10,9 +10,15 @@
#include "ipfs/core/daemon.h"
#include "ipfs/routing/routing.h"
#include "ipfs/core/ipfs_node.h"
#include "libp2p/secio/secio.h"
#define BUF_SIZE 4096
int ipfs_null_requesting_secio(unsigned char* buffer, size_t buffer_size) {
if (buffer_size > 5 && strncmp((char*)buffer, "/secio", 6) == 0)
return 1;
return 0;
}
/**
* We've received a connection. Find out what they want
*/
@ -35,28 +41,43 @@ void *ipfs_null_connection (void *ptr)
routing = ipfs_routing_new_online(connection_param->local_node, &connection_param->local_node->identity->private_key, stream);
for(;;) {
struct Libp2pMessage* msg = libp2p_net_multistream_get_message(stream);
if (msg != NULL) {
switch(msg->message_type) {
case (MESSAGE_TYPE_PING):
routing->Ping(routing, msg);
break;
case (MESSAGE_TYPE_GET_VALUE): {
unsigned char* val;
size_t val_size = 0;
routing->GetValue(routing, msg->key, msg->key_size, (void**)&val, &val_size);
if (val == NULL) {
stream->write(stream, 0, 1);
} else {
stream->write(stream, val, val_size);
}
break;
}
default:
// 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;
if (!libp2p_secio_handshake(&secure_session, &connection_param->local_node->identity->private_key)) {
// rejecting connection
break;
}
} else {
break;
struct Libp2pMessage* msg = NULL;
libp2p_message_protobuf_decode(results, bytes_read, &msg);
if (msg != NULL) {
switch(msg->message_type) {
case (MESSAGE_TYPE_PING):
routing->Ping(routing, msg);
break;
case (MESSAGE_TYPE_GET_VALUE): {
unsigned char* val;
size_t val_size = 0;
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);
} else {
stream->write(stream, val, val_size);
}
break;
}
default:
break;
}
} else {
break;
}
}
}
}

View file

@ -9,6 +9,7 @@
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "libp2p/net/multistream.h"
#include "libp2p/record/message.h"
#include "libp2p/utils/multiaddress.h"
/**
* return the next chunk of a path
@ -95,27 +96,6 @@ int ipfs_resolver_is_remote(const char* path, const struct FSRepo* fs_repo) {
}
/**
* This is a hack to get ip4/tcp working
* TODO: this should be moved further down in the networking stack and generified for different multiaddresses
* This makes too many assumptions
*/
int ipfs_resolver_multiaddress_parse_tcp(struct MultiAddress* address, char** ip, int* port) {
// ip
char* str = malloc(strlen(address->string));
strcpy(str, &address->string[5]); // gets rid of /ip4/
char* pos = strchr(str, '/');
pos[0] = 0;
*ip = malloc(strlen(str) + 1);
strcpy(*ip, str);
free(str);
// port
str = strstr(address->string, "/tcp/");
str += 5;
*port = atoi(str);
return 1;
}
/**
* Retrieve a node from a remote source
* @param path the path to retrieve
@ -148,7 +128,7 @@ struct Node* ipfs_resolver_remote_get(const char* path, struct Node* from, const
struct MultiAddress* address = peer->addr_head->item;
char* ip;
int port;
ipfs_resolver_multiaddress_parse_tcp(address, &ip, &port);
libp2p_utils_multiaddress_parse_ip4_tcp(address, &ip, &port);
struct Stream* stream = libp2p_net_multistream_connect(ip, port);
free(ip);
// build the request