Adding network calls to kademlia

This commit is contained in:
John Jones 2017-03-30 13:59:31 -05:00
parent 59af1c0b9e
commit 96b97ad347
4 changed files with 29 additions and 13 deletions

View file

@ -47,11 +47,12 @@ int ipfs_daemon_start(char* repo_path) {
return 1; return 1;
} }
// create pthread for the API /*
//ipfs_bootstrap_routing(&local_node); //ipfs_bootstrap_routing(&local_node);
if (pthread_create(&work_pths[count_pths++], NULL, ipfs_bootstrap_routing, &local_node)) { if (pthread_create(&work_pths[count_pths++], NULL, ipfs_bootstrap_routing, &local_node)) {
fprintf(stderr, "Error creating thread for routing\n"); fprintf(stderr, "Error creating thread for routing\n");
} }
*/
fprintf(stderr, "Daemon is ready\n"); fprintf(stderr, "Daemon is ready\n");

View file

@ -12,28 +12,33 @@
#include "ipfs/core/ipfs_node.h" #include "ipfs/core/ipfs_node.h"
#include "libp2p/secio/secio.h" #include "libp2p/secio/secio.h"
#include "libp2p/nodeio/nodeio.h" #include "libp2p/nodeio/nodeio.h"
#include "libp2p/routing/dht_protocol.h"
#include "ipfs/merkledag/merkledag.h" #include "ipfs/merkledag/merkledag.h"
#include "ipfs/merkledag/node.h" #include "ipfs/merkledag/node.h"
#define BUF_SIZE 4096 #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) * Compare incoming to see if they are requesting a protocol upgrade
return 1; * @param incoming the incoming string
return 0; * @param incoming_size the size of the incoming string
} * @param test the protocol string to compare it with (i.e. "/secio" or "/nodeio"
int ipfs_null_requesting_nodeio(unsigned char* buffer, size_t buffer_size) { * @returns true(1) if there was a match, false(0) otherwise
if (buffer_size > 5 && strncmp((char*)buffer, "/nodeio", 7) == 0) */
int protocol_compare(unsigned char* incoming, size_t incoming_size, const char* test) {
int test_size = strlen(test);
if (incoming_size >= test_size && strncmp((char*)incoming, test, test_size) == 0)
return 1; return 1;
return 0; return 0;
} }
/** /**
* We've received a connection. Find out what they want * We've received a connection. Find out what they want
*/ */
void *ipfs_null_connection (void *ptr) void *ipfs_null_connection (void *ptr)
{ {
struct null_connection_params *connection_param = NULL; struct null_connection_params *connection_param = NULL;
struct s_ipfs_routing* routing = NULL; //struct s_ipfs_routing* routing = NULL;
connection_param = (struct null_connection_params*) ptr; connection_param = (struct null_connection_params*) ptr;
@ -47,19 +52,19 @@ void *ipfs_null_connection (void *ptr)
fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count)); fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count));
if (libp2p_net_multistream_negotiate(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); //routing = ipfs_routing_new_online(connection_param->local_node, &connection_param->local_node->identity->private_key, session.insecure_stream);
for(;;) { for(;;) {
// check if they're looking for an upgrade (i.e. secio) // check if they're looking for an upgrade (i.e. secio)
unsigned char* results = NULL; unsigned char* results = NULL;
size_t bytes_read; size_t bytes_read;
session.default_stream->read(&session, &results, &bytes_read); session.default_stream->read(&session, &results, &bytes_read);
if (ipfs_null_requesting_secio(results, bytes_read)) { if (protocol_compare(results, bytes_read, "/secio")) {
if (!libp2p_secio_handshake(&session, &connection_param->local_node->identity->private_key, 1)) { if (!libp2p_secio_handshake(&session, &connection_param->local_node->identity->private_key, 1)) {
// rejecting connection // rejecting connection
break; break;
} }
} else if (ipfs_null_requesting_nodeio(results, bytes_read)) { } else if (protocol_compare(results, bytes_read, "/nodeio")) {
if (!libp2p_nodeio_handshake(&session)) if (!libp2p_nodeio_handshake(&session))
break; break;
// loop through file requests // loop through file requests
@ -89,8 +94,16 @@ void *ipfs_null_connection (void *ptr)
session.default_stream->write(&session, results, results_size); session.default_stream->write(&session, results, results_size);
} }
} }
} else if (protocol_compare(results, bytes_read, "/kad/")) {
if (!libp2p_routing_dht_handshake(&session))
break;
// this handles 1 transaction
libp2p_routing_dht_handle_message(&session, connection_param->local_node->peerstore, connection_param->local_node->providerstore);
} }
else { else {
// oops there was a problem
//TODO: Handle this
/*
struct Libp2pMessage* msg = NULL; struct Libp2pMessage* msg = NULL;
libp2p_message_protobuf_decode(results, bytes_read, &msg); libp2p_message_protobuf_decode(results, bytes_read, &msg);
if (msg != NULL) { if (msg != NULL) {
@ -116,6 +129,7 @@ void *ipfs_null_connection (void *ptr)
} else { } else {
break; break;
} }
*/
} }
} }
} }

View file

@ -2,6 +2,7 @@
#define DAEMON_H #define DAEMON_H
#include <stdint.h> #include <stdint.h>
#include "ipfs/core/ipfs_node.h"
#define MAX 5 #define MAX 5
#define CONNECTIONS 50 #define CONNECTIONS 50

View file

@ -1,5 +1,5 @@
CC = gcc CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/ -I../../c-protobuf -g3 -Wall -std=c99 CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -g3 -Wall -std=c99
LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr -lpthread -lresolv LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr -lpthread -lresolv
DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h
OBJS = main.o \ OBJS = main.o \