diff --git a/main/main.c b/main/main.c index 6234324..4d86cab 100644 --- a/main/main.c +++ b/main/main.c @@ -112,6 +112,7 @@ int main(int argc, char** argv) { break; case(GET): //ipfs_exporter_get(argc, argv); + //break; case (CAT): ipfs_exporter_object_cat(argc, argv); break; diff --git a/routing/k_routing.c b/routing/k_routing.c index bec9a78..19b3fc1 100644 --- a/routing/k_routing.c +++ b/routing/k_routing.c @@ -98,12 +98,13 @@ int ipfs_routing_kademlia_bootstrap(struct s_ipfs_routing* routing) { } struct s_ipfs_routing* ipfs_routing_new_kademlia(struct IpfsNode* local_node, struct RsaPrivateKey* private_key, struct Stream* stream) { - char* kademlia_id = NULL; - // generate kademlia compatible id by getting last 20 chars of peer id + char kademlia_id[21]; + // generate kademlia compatible id by getting first 20 chars of peer id if (local_node->identity->peer_id == NULL || strlen(local_node->identity->peer_id) < 20) { return NULL; } - kademlia_id = &local_node->identity->peer_id[strlen(local_node->identity->peer_id)-20]; + strncpy(kademlia_id, local_node->identity->peer_id, 20); + kademlia_id[20] = 0; struct s_ipfs_routing* routing = (struct s_ipfs_routing*)malloc(sizeof(struct s_ipfs_routing)); if (routing != NULL) { routing->local_node = local_node; diff --git a/test/routing/test_supernode.h b/test/routing/test_supernode.h index 94a93f6..aa99cfa 100644 --- a/test/routing/test_supernode.h +++ b/test/routing/test_supernode.h @@ -9,6 +9,7 @@ #include "libp2p/utils/linked_list.h" #include "libp2p/peer/peerstore.h" #include "libp2p/peer/providerstore.h" +#include "libp2p/crypto/encoding/base58.h" void stop_kademlia(void); @@ -45,6 +46,101 @@ void* start_daemon(void* path) { return NULL; } +int test_routing_supernode_get_remote_value() { + // a remote machine has a file. Let's see if we can get it. + // the key is QmXQDbRPsbGdtvDwWvXVXb9TwUTNMcGNfWWnA9oSJJBi5n, which is the email.txt from jmjatlanta.com + int retVal = 0; + struct FSRepo* fs_repo = NULL; + struct IpfsNode* ipfs_node = NULL; + struct Libp2pPeer this_peer; + struct Stream* stream = NULL; + const unsigned char* orig_multihash = (unsigned char*)"QmXQDbRPsbGdtvDwWvXVXb9TwUTNMcGNfWWnA9oSJJBi5n"; + size_t hash_size = 100; + unsigned char hash[hash_size]; + unsigned char* hash_ptr = &hash[0]; + struct Libp2pVector* multiaddresses; + struct MultiAddress* addr = NULL; + char* ip = NULL; + struct SessionContext context; + unsigned char* results = NULL; + size_t results_size = 0; + struct Node* node; + + // unencode the base58 + if (!libp2p_crypto_encoding_base58_decode(orig_multihash, strlen((char*)orig_multihash), &hash_ptr, &hash_size)) + goto exit; + + // fire things up + if (!drop_build_and_open_repo("/tmp/.ipfs", &fs_repo)) + goto exit; + + ipfs_node = (struct IpfsNode*)malloc(sizeof(struct IpfsNode)); + ipfs_node->mode = MODE_ONLINE; + ipfs_node->identity = fs_repo->config->identity; + ipfs_node->repo = fs_repo; + ipfs_node->providerstore = libp2p_providerstore_new(); + ipfs_node->peerstore = libp2p_peerstore_new(); + // add the local peer to the peerstore + this_peer.id = fs_repo->config->identity->peer_id; + this_peer.id_size = strlen(fs_repo->config->identity->peer_id); + this_peer.addr_head = libp2p_utils_linked_list_new(); + this_peer.addr_head->item = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/4001"); + libp2p_peerstore_add_peer(ipfs_node->peerstore, &this_peer); + ipfs_node->routing = ipfs_routing_new_kademlia(ipfs_node, &fs_repo->config->identity->private_key, stream); + + if (ipfs_node->routing == NULL) + goto exit; + + // ask the network who can provide this + if (!ipfs_node->routing->FindProviders(ipfs_node->routing, (char*)hash, hash_size, &multiaddresses)) + goto exit; + + // get the file + for(int i = 0; i < multiaddresses->total; i++) { + addr = (struct MultiAddress*) libp2p_utils_vector_get(multiaddresses, i); + if (multiaddress_is_ip4(addr)) { + break; + } + addr = NULL; + } + + if (addr == NULL) + goto exit; + + // Connect to server + multiaddress_get_ip_address(addr, &ip); + struct Stream* file_stream = libp2p_net_multistream_connect(ip, multiaddress_get_ip_port(addr)); + + if (file_stream == NULL) + goto exit; + + context.insecure_stream = file_stream; + context.default_stream = file_stream; + // Switch from multistream to NodeIO + if (!libp2p_nodeio_upgrade_stream(&context)) + goto exit; + + // Ask for file + if (!libp2p_nodeio_get(&context, hash, hash_size, &results, &results_size)) + goto exit; + + if (!ipfs_node_protobuf_decode(results, results_size, &node)) + goto exit; + + //we got it + if (node->data_size < 100) + goto exit; + // make sure we got what we should have gotten + // clean up + retVal = 1; + exit: + if (fs_repo != NULL) + ipfs_repo_fsrepo_free(fs_repo); + if (ipfs_node != NULL) + free(ipfs_node); + return retVal; +} + int test_routing_supernode_get_value() { int retVal = 0; struct FSRepo* fs_repo = NULL; diff --git a/test/testit.c b/test/testit.c index a38e283..877ddab 100644 --- a/test/testit.c +++ b/test/testit.c @@ -58,6 +58,7 @@ const char* names[] = { "test_merkledag_add_node_with_links", "test_resolver_get", "test_routing_supernode_get_value", + "test_routing_supernode_get_remote_value", "test_unixfs_encode_decode", "test_unixfs_encode_smallfile", "test_ping", @@ -96,6 +97,7 @@ int (*funcs[])(void) = { test_merkledag_add_node_with_links, test_resolver_get, test_routing_supernode_get_value, + test_routing_supernode_get_remote_value, test_unixfs_encode_decode, test_unixfs_encode_smallfile, test_ping,