c-ipfs/core/null.c

209 lines
6.8 KiB
C
Raw Normal View History

2017-02-10 01:10:21 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
2017-02-10 01:10:21 +00:00
#include <unistd.h>
#include <pthread.h>
2017-04-04 01:54:03 +00:00
#include <netinet/in.h>
#include <arpa/inet.h>
2017-02-10 01:10:21 +00:00
#include "libp2p/net/p2pnet.h"
#include "libp2p/record/message.h"
#include "libp2p/net/multistream.h"
2017-04-03 18:20:35 +00:00
#include "libp2p/utils/logger.h"
2017-02-10 01:10:21 +00:00
#include "ipfs/core/daemon.h"
#include "ipfs/routing/routing.h"
#include "ipfs/core/ipfs_node.h"
2017-03-02 21:18:02 +00:00
#include "libp2p/secio/secio.h"
2017-03-19 19:40:16 +00:00
#include "libp2p/nodeio/nodeio.h"
2017-03-30 18:59:31 +00:00
#include "libp2p/routing/dht_protocol.h"
2017-03-19 19:40:16 +00:00
#include "ipfs/merkledag/merkledag.h"
#include "ipfs/merkledag/node.h"
#include "ipfs/util/thread_pool.h"
2017-02-10 01:10:21 +00:00
#define BUF_SIZE 4096
static int null_shutting_down = 0;
2017-03-30 18:59:31 +00:00
/***
* Compare incoming to see if they are requesting a protocol upgrade
* @param incoming the incoming string
* @param incoming_size the size of the incoming string
* @param test the protocol string to compare it with (i.e. "/secio" or "/nodeio"
* @returns true(1) if there was a match, false(0) otherwise
*/
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)
2017-03-19 19:40:16 +00:00
return 1;
return 0;
}
2017-03-30 18:59:31 +00:00
/**
* We've received a connection. Find out what they want
*/
void ipfs_null_connection (void *ptr)
2017-02-10 01:10:21 +00:00
{
struct null_connection_params *connection_param = NULL;
2017-02-10 01:10:21 +00:00
connection_param = (struct null_connection_params*) ptr;
// TODO: when should we exit the for loop and disconnect?
2017-03-19 19:40:16 +00:00
struct SessionContext session;
2017-04-04 01:54:03 +00:00
session.insecure_stream = libp2p_net_multistream_stream_new(connection_param->file_descriptor, connection_param->ip, connection_param->port);
2017-03-19 19:40:16 +00:00
session.default_stream = session.insecure_stream;
session.datastore = connection_param->local_node->repo->config->datastore;
session.filestore = connection_param->local_node->repo->config->filestore;
2017-04-04 01:54:03 +00:00
libp2p_logger_log("null", LOGLEVEL_INFO, "Connection %d, count %d\n", connection_param->file_descriptor, *(connection_param->count));
2017-02-10 01:10:21 +00:00
2017-03-19 19:40:16 +00:00
if (libp2p_net_multistream_negotiate(session.insecure_stream)) {
for(;;) {
2017-03-02 21:18:02 +00:00
// check if they're looking for an upgrade (i.e. secio)
unsigned char* results = NULL;
2017-04-03 22:26:33 +00:00
size_t bytes_read = 0;
if (!session.default_stream->read(&session, &results, &bytes_read, 5) ) {
2017-04-03 22:26:33 +00:00
libp2p_logger_debug("null", "stream transaction read returned false\n");
break;
2017-04-03 18:41:26 +00:00
}
if (null_shutting_down) {
break;
}
if (bytes_read == 0) { // timeout
continue;
}
2017-04-03 22:26:33 +00:00
libp2p_logger_debug("null", "Read %lu bytes from a stream tranaction\n", bytes_read);
2017-03-30 18:59:31 +00:00
if (protocol_compare(results, bytes_read, "/secio")) {
2017-04-03 18:20:35 +00:00
libp2p_logger_debug("null", "Attempting secure io connection...\n");
2017-03-19 19:40:16 +00:00
if (!libp2p_secio_handshake(&session, &connection_param->local_node->identity->private_key, 1)) {
2017-03-02 21:18:02 +00:00
// rejecting connection
2017-04-03 18:20:35 +00:00
libp2p_logger_debug("null", "Secure IO connection failed\n");
free(results);
break;
2017-02-27 17:27:40 +00:00
}
2017-03-30 18:59:31 +00:00
} else if (protocol_compare(results, bytes_read, "/nodeio")) {
2017-04-03 18:20:35 +00:00
libp2p_logger_debug("null", "Attempting a nodeio connection.\n");
if (!libp2p_nodeio_handshake(&session)) {
free(results);
2017-03-19 19:40:16 +00:00
break;
}
2017-03-19 19:40:16 +00:00
// 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, 5);
2017-03-19 19:40:16 +00:00
if (hash_length < 20) {
_continue = 0;
continue;
}
else {
// try to get the Node
struct HashtableNode* node = NULL;
2017-03-19 19:40:16 +00:00
if (!ipfs_merkledag_get(hash, hash_length, &node, connection_param->local_node->repo)) {
_continue = 0;
continue;
}
size_t results_size = ipfs_hashtable_node_protobuf_encode_size(node);
2017-03-19 19:40:16 +00:00
unsigned char results[results_size];
if (!ipfs_hashtable_node_protobuf_encode(node, results, results_size, &results_size)) {
2017-03-19 19:40:16 +00:00
_continue = 0;
continue;
}
// send it to the requestor
session.default_stream->write(&session, results, results_size);
}
}
2017-04-03 18:26:11 +00:00
} else if (protocol_compare(results, bytes_read, "/ipfs/kad/")) {
2017-04-03 18:20:35 +00:00
libp2p_logger_log("null", LOGLEVEL_DEBUG, "Attempting kademlia connection...\n");
2017-04-03 17:42:35 +00:00
if (!libp2p_routing_dht_handshake(&session)) {
2017-04-03 18:20:35 +00:00
libp2p_logger_log("null", LOGLEVEL_DEBUG, "kademlia connection handshake failed\n");
free(results);
2017-03-30 18:59:31 +00:00
break;
2017-04-03 17:42:35 +00:00
}
2017-03-30 18:59:31 +00:00
// this handles 1 transaction
libp2p_routing_dht_handle_message(&session, connection_param->local_node->peerstore, connection_param->local_node->providerstore);
2017-04-03 18:20:35 +00:00
libp2p_logger_log("null", LOGLEVEL_DEBUG, "kademlia message handled\n");
2017-03-19 19:40:16 +00:00
}
else {
libp2p_logger_error("null", "There was a problem with this connection. It is nothing I can handle. Disconnecting.\n");
break;
}
free(results);
}
2017-04-03 17:42:35 +00:00
} else {
2017-04-03 18:20:35 +00:00
libp2p_logger_log("null", LOGLEVEL_DEBUG, "Multistream negotiation failed\n");
}
2017-03-19 19:40:16 +00:00
if (session.default_stream != NULL) {
session.default_stream->close(&session);
}
if (session.insecure_stream != NULL) {
libp2p_net_multistream_stream_free(session.insecure_stream);
}
2017-02-10 01:10:21 +00:00
(*(connection_param->count))--; // update counter.
if (connection_param->ip != NULL)
free(connection_param->ip);
2017-02-10 01:10:21 +00:00
free (connection_param);
return;
2017-02-10 01:10:21 +00:00
}
void *ipfs_null_listen (void *ptr)
{
int socketfd, s, count = 0;
threadpool thpool = thpool_init(25);
struct IpfsNodeListenParams *listen_param;
2017-02-10 01:10:21 +00:00
struct null_connection_params *connection_param;
listen_param = (struct IpfsNodeListenParams*) ptr;
2017-02-10 01:10:21 +00:00
if ((socketfd = socket_listen(socket_tcp4(), &(listen_param->ipv4), &(listen_param->port))) <= 0) {
perror("fail to init null router.");
exit (1);
}
libp2p_logger_log("null", LOGLEVEL_ERROR, "Ipfs listening on %d\n", listen_param->port);
2017-02-10 01:10:21 +00:00
for (;;) {
int numDescriptors = socket_read_select4(socketfd, 5);
if (null_shutting_down) {
break;
}
if (numDescriptors > 0) {
s = socket_accept4(socketfd, &(listen_param->ipv4), &(listen_param->port));
if (count >= CONNECTIONS) { // limit reached.
close (s);
continue;
}
count++;
connection_param = malloc (sizeof (struct null_connection_params));
if (connection_param) {
connection_param->file_descriptor = s;
connection_param->count = &count;
connection_param->local_node = listen_param->local_node;
connection_param->port = listen_param->port;
connection_param->ip = malloc(INET_ADDRSTRLEN);
if (inet_ntop(AF_INET, &(listen_param->ipv4), connection_param->ip, INET_ADDRSTRLEN) == NULL) {
free(connection_param->ip);
connection_param->ip = NULL;
connection_param->port = 0;
}
// Create pthread for ipfs_null_connection.
thpool_add_work(thpool, ipfs_null_connection, connection_param);
}
}
2017-02-10 01:10:21 +00:00
}
thpool_destroy(thpool);
2017-02-10 01:10:21 +00:00
return (void*) 2;
}
int ipfs_null_shutdown() {
null_shutting_down = 1;
return 1;
}