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