More implementation of dialer

This commit is contained in:
jmjatlanta 2017-10-23 18:03:55 -05:00
parent 9087c58113
commit b72f247939
2 changed files with 18 additions and 22 deletions

View file

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h>
#include "ipfs/importer/resolver.h" #include "ipfs/importer/resolver.h"
#include "libp2p/utils/logger.h" #include "libp2p/utils/logger.h"
@ -15,6 +16,7 @@
#include "libp2p/record/message.h" #include "libp2p/record/message.h"
#include "multiaddr/multiaddr.h" #include "multiaddr/multiaddr.h"
#include "libp2p/record/message.h" #include "libp2p/record/message.h"
#include "libp2p/conn/dialer.h"
/** /**
* return the next chunk of a path * return the next chunk of a path
@ -138,20 +140,18 @@ struct HashtableNode* ipfs_resolver_remote_get(const char* path, struct Hashtabl
//TODO: We don't have the peer address. Ask the swarm for the data related to the hash //TODO: We don't have the peer address. Ask the swarm for the data related to the hash
return NULL; return NULL;
} }
// connect to the peer if (!libp2p_peer_connect(ipfs_node->dialer, peer, ipfs_node->peerstore, ipfs_node->repo->config->datastore, 10))
struct MultiAddress* address = peer->addr_head->item; return NULL;
char* ip; struct Stream* kademlia_stream = libp2p_conn_dialer_get_stream(ipfs_node->dialer, peer, "kademlia");
int port = multiaddress_get_ip_port(address); if (kademlia_stream == NULL)
multiaddress_get_ip_address(address, &ip); return NULL;
struct Stream* stream = libp2p_net_multistream_connect(ip, port);
free(ip);
// build the request // build the request
struct KademliaMessage* message = libp2p_message_new(); struct KademliaMessage* message = libp2p_message_new();
message->message_type = MESSAGE_TYPE_GET_VALUE; message->message_type = MESSAGE_TYPE_GET_VALUE;
message->key = key; message->key = key;
message->key_size = strlen(key); message->key_size = strlen(key);
size_t b58size = 100; size_t b58size = 100;
uint8_t *b58key = (uint8_t *) malloc(b58size); uint8_t *b58key = (uint8_t*) malloc(b58size);
if (b58key == NULL) { if (b58key == NULL) {
libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size); libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size);
libp2p_logger_debug("resolver", "Attempting to use kademlia to get key %s.\n", b58key); libp2p_logger_debug("resolver", "Attempting to use kademlia to get key %s.\n", b58key);
@ -161,19 +161,13 @@ struct HashtableNode* ipfs_resolver_remote_get(const char* path, struct Hashtabl
unsigned char message_protobuf[message_protobuf_size]; unsigned char message_protobuf[message_protobuf_size];
libp2p_message_protobuf_encode(message, message_protobuf, message_protobuf_size, &message_protobuf_size); libp2p_message_protobuf_encode(message, message_protobuf, message_protobuf_size, &message_protobuf_size);
libp2p_message_free(message); libp2p_message_free(message);
struct SessionContext session_context;
session_context.insecure_stream = stream;
session_context.default_stream = stream;
// switch to kademlia
if (!libp2p_routing_dht_upgrade_stream(&session_context))
return NULL;
struct StreamMessage outgoing; struct StreamMessage outgoing;
outgoing.data = message_protobuf; outgoing.data = message_protobuf;
outgoing.data_size = message_protobuf_size; outgoing.data_size = message_protobuf_size;
stream->write(&session_context, &outgoing); kademlia_stream->write(kademlia_stream->stream_context, &outgoing);
struct StreamMessage* response; struct StreamMessage* response;
// we should get back a protobuf'd record // we should get back a protobuf'd record
stream->read(&session_context, &response, 5); kademlia_stream->read(kademlia_stream->stream_context, &response, 5);
if (response->data_size == 1) if (response->data_size == 1)
return NULL; return NULL;
// turn the protobuf into a Node // turn the protobuf into a Node

View file

@ -23,11 +23,9 @@ int test_ping() {
//struct IpfsNode local_node; //struct IpfsNode local_node;
struct Libp2pPeer* remote_peer = NULL; struct Libp2pPeer* remote_peer = NULL;
struct Dialer* dialer = NULL; struct Dialer* dialer = NULL;
struct Connection* conn = NULL; struct Stream* conn = NULL;
unsigned char* protobuf = NULL; unsigned char* protobuf = NULL;
size_t protobuf_size = 0; size_t protobuf_size = 0;
unsigned char* response = NULL;
size_t response_size = 0;
// act like this is a normal node // act like this is a normal node
drop_build_and_open_repo("/tmp/.ipfs", &fs_repo); drop_build_and_open_repo("/tmp/.ipfs", &fs_repo);
@ -63,9 +61,13 @@ int test_ping() {
//TODO: Dialer should know the protocol //TODO: Dialer should know the protocol
// send the record // send the record
conn->write(conn, (char*)protobuf, protobuf_size); struct StreamMessage msg;
conn->read(conn, (char**)&response, &response_size); msg.data = (uint8_t*)protobuf;
libp2p_message_protobuf_decode(response, response_size, &message); msg.data_size = protobuf_size;
conn->write(conn->stream_context, &msg);
struct StreamMessage* incoming_message = NULL;
conn->read(conn->stream_context, &incoming_message, 10);
libp2p_message_protobuf_decode(incoming_message->data, incoming_message->data_size, &message);
// verify the response // verify the response
if (message->message_type != MESSAGE_TYPE_PING) if (message->message_type != MESSAGE_TYPE_PING)
goto exit; goto exit;