Working with secio streams

This commit is contained in:
John Jones 2017-03-09 12:50:08 -05:00
parent 15a8abff9a
commit 43ca313854
8 changed files with 96 additions and 12 deletions

View file

@ -7,7 +7,7 @@ endif
LFLAGS =
DEPS = builder.h ipfs_node.h
OBJS = builder.o daemon.o null.o ping.o
OBJS = builder.o daemon.o null.o ping.o bootstrap.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

30
core/bootstrap.c Normal file
View file

@ -0,0 +1,30 @@
#include "libp2p/peer/peer.h"
#include "ipfs/core/ipfs_node.h"
#include "ipfs/thirdparty/ipfsaddr/ipfs_addr.h"
#include "multiaddr/multiaddr.h"
/***
* Begin to connect to the swarm
*/
void *ipfs_bootstrap_swarm(void* param) {
struct IpfsNode* local_node = (struct IpfsNode*)param;
// read the config file and get the bootstrap peers
for(int i = 0; i < local_node->repo->config->peer_addresses.num_peers; i++) { // loop through the peers
struct IPFSAddr* ipfs_addr = local_node->repo->config->peer_addresses.peers[i];
struct MultiAddress* ma = multiaddress_new_from_string(ipfs_addr->entire_string);
// get the id
char* ptr;
if ( (ptr = strstr(ipfs_addr->entire_string, "/ipfs/")) != NULL) { // look for the peer id
ptr += 6;
if (ptr[0] == 'Q' && ptr[1] == 'm') { // things look good
struct Libp2pPeer* peer = libp2p_peer_new_from_data(ptr, strlen(ptr), ma);
libp2p_peerstore_add_peer(local_node->peerstore, peer);
}
// TODO: attempt to connect to the peer
} // we have a good peer ID
}
return (void*)1;
}

View file

@ -7,6 +7,7 @@
#include "libp2p/peer/peerstore.h"
#include "ipfs/core/daemon.h"
#include "ipfs/core/ipfs_node.h"
#include "ipfs/core/bootstrap.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
int ipfs_daemon_start(char* repo_path) {
@ -45,6 +46,11 @@ int ipfs_daemon_start(char* repo_path) {
return 1;
}
// create pthread for connecting to the swarm bootstrap
if (pthread_create(&work_pths[count_pths++], NULL, ipfs_bootstrap_swarm, &local_node)) {
}
fprintf(stderr, "Daemon is ready\n");
// Wait for pthreads to finish.

View file

@ -46,7 +46,7 @@ void *ipfs_null_connection (void *ptr)
unsigned char* results = NULL;
size_t bytes_read;
secure_session.default_stream->read(&secure_session, &results, &bytes_read);
if (secure_session.secure_stream == NULL && ipfs_null_requesting_secio(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)) {
// rejecting connection
break;

View file

@ -7,6 +7,8 @@
#include "libp2p/net/p2pnet.h"
#include "libp2p/net/multistream.h"
#include "libp2p/record/message.h"
#include "libp2p/secio/secio.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#define BUF_SIZE 4096
@ -14,17 +16,42 @@ int ipfs_ping (int argc, char **argv)
{
unsigned char* results = NULL;
size_t results_size = 0;
//TODO: handle multiaddress
int port = 0;
char* ip = NULL;
struct SecureSession session;
// the way using multistream
//TODO: Error checking
char* ip = argv[2];
int port = atoi(argv[3]);
struct Stream* stream = libp2p_net_multistream_connect(ip, port);
if (stream == NULL) {
// read the configuration
struct FSRepo* fs_repo;
if (!ipfs_repo_fsrepo_new(NULL, NULL, &fs_repo))
return 0;
// open the repository and read the file
if (!ipfs_repo_fsrepo_open(fs_repo)) {
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
//TODO: handle multiaddress
if (strstr(argv[2], "/ipfs/") != NULL) {
// look in peerstore
} else {
// the way using multistream
//TODO: Error checking
ip = argv[2];
port = atoi(argv[3]);
}
session.insecure_stream = libp2p_net_multistream_connect(ip, port);
session.default_stream = session.insecure_stream;
if (session.insecure_stream == NULL) {
fprintf(stderr, "Unable to connect to %s on port %s", ip, argv[3]);
}
// try to switch to secio
if (!libp2p_secio_handshake(&session, &fs_repo->config->identity->private_key, 0)) {
fprintf(stderr, "Unable to switch to secure connection. Attempting insecure ping...\n");
}
// prepare the PING message
struct Libp2pMessage* msg = libp2p_message_new();
msg->message_type = MESSAGE_TYPE_PING;
@ -32,8 +59,16 @@ int ipfs_ping (int argc, char **argv)
size_t protobuf_size = libp2p_message_protobuf_encode_size(msg);
unsigned char protobuf[protobuf_size];
libp2p_message_protobuf_encode(msg, &protobuf[0], protobuf_size, &protobuf_size);
libp2p_net_multistream_write(stream, protobuf, protobuf_size);
libp2p_net_multistream_read(stream, &results, &results_size);
session.default_stream->write(&session, protobuf, protobuf_size);
session.default_stream->read(&session, &results, &results_size);
// see if we can unprotobuf
struct Libp2pMessage* msg_returned = NULL;
libp2p_message_protobuf_decode(results, results_size, &msg_returned);
if (msg_returned->message_type != MESSAGE_TYPE_PING) {
fprintf(stderr, "Ping unsuccessful. Returned message was not a PING");
return 0;
}
if (results_size != protobuf_size) {
fprintf(stderr, "PING unsuccessful. Original size: %lu, returned size: %lu\n", protobuf_size, results_size);

View file

@ -0,0 +1,12 @@
#pragma once
/**
* Fires up the connection to peers
*/
/**
* Connect to the peers in the config file
* @param param a IpfsNode object
* @returns nothing useful
*/
void *ipfs_bootstrap_swarm(void* param);

View file

@ -7,7 +7,7 @@ OBJS = main.o \
../cid/cid.o \
../cmd/ipfs/init.o \
../commands/argument.o ../commands/command_option.o ../commands/command.o ../commands/cli/parse.o \
../core/builder.o ../core/daemon.o ../core/null.o ../core/ping.o \
../core/builder.o ../core/daemon.o ../core/null.o ../core/ping.o ../core/bootstrap.o \
../datastore/ds_helper.o \
../datastore/key.o \
../dnslink/*.o \

View file

@ -10,6 +10,7 @@ OBJS = testit.o test_helper.o \
../core/builder.o \
../core/daemon.o \
../core/null.o \
../core/bootstrap.o \
../datastore/ds_helper.o \
../flatfs/flatfs.o \
../importer/importer.o ../importer/exporter.o ../importer/resolver.o \