diff --git a/core/bootstrap.c b/core/bootstrap.c index b009455..49f9bd7 100644 --- a/core/bootstrap.c +++ b/core/bootstrap.c @@ -9,6 +9,8 @@ * Begin to connect to the swarm */ void *ipfs_bootstrap_swarm(void* param) { + //TODO: + /* 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 @@ -27,6 +29,7 @@ void *ipfs_bootstrap_swarm(void* param) { } // we have a good peer ID } + */ return (void*)1; } diff --git a/include/ipfs/repo/config/bootstrap_peers.h b/include/ipfs/repo/config/bootstrap_peers.h index a468598..1aba24a 100644 --- a/include/ipfs/repo/config/bootstrap_peers.h +++ b/include/ipfs/repo/config/bootstrap_peers.h @@ -1,25 +1,20 @@ #ifndef bootstrap_peer_h #define bootstrap_peer_h -#include "ipfs/thirdparty/ipfsaddr/ipfs_addr.h" - -struct BootstrapPeers { - int num_peers; - struct IPFSAddr** peers; -}; +#include "libp2p/utils/vector.h" /*** * get a list of peers to use to bootstrap the instance - * @param bootstrap_peers an array of IPFSAddr structs, will be allocated by this function + * @param bootstrap_peers A vector of MultiAddress structs will be allocated by this function * @returns true(1) on success, otherwise false(0) */ -int repo_config_bootstrap_peers_retrieve(struct BootstrapPeers* bootstrap_peers); +int repo_config_bootstrap_peers_retrieve(struct Libp2pVector** bootstrap_peers); /*** * frees up memory caused by call to repo_config_bootstrap_peers_retrieve * @param list the list to free * @returns true(1) */ -int repo_config_bootstrap_peers_free(struct BootstrapPeers* list); +int repo_config_bootstrap_peers_free(struct Libp2pVector* list); #endif /* bootstrap_peer_h */ diff --git a/include/ipfs/repo/config/config.h b/include/ipfs/repo/config/config.h index 9c7bf3f..86ecf11 100644 --- a/include/ipfs/repo/config/config.h +++ b/include/ipfs/repo/config/config.h @@ -37,7 +37,7 @@ struct RepoConfig { struct Mounts mounts; struct Discovery discovery; struct Ipns ipns; - struct BootstrapPeers peer_addresses; + struct Libp2pVector* bootstrap_peers; // MultiAddresses //struct tour tour; struct Gateway* gateway; //struct supernode_routing supernode_client_config; diff --git a/repo/config/Makefile b/repo/config/Makefile index dab8b90..576f8f0 100644 --- a/repo/config/Makefile +++ b/repo/config/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -O0 -I../../include -I../../../c-libp2p/include -I../../../c-multihash/include -I../../../c-protobuf -Wall -std=c99 +CFLAGS = -O0 -I../../include -I../../../c-libp2p/include -I../../../c-multiaddr/include -I../../../c-multihash/include -I../../../c-protobuf -Wall -std=c99 ifdef DEBUG CFLAGS += -g3 diff --git a/repo/config/bootstrap_peers.c b/repo/config/bootstrap_peers.c index 30a15d3..8afa059 100644 --- a/repo/config/bootstrap_peers.c +++ b/repo/config/bootstrap_peers.c @@ -1,10 +1,10 @@ #include #include -#include "ipfs/thirdparty/ipfsaddr/ipfs_addr.h" #include "ipfs/repo/config/bootstrap_peers.h" +#include "multiaddr/multiaddr.h" -int repo_config_bootstrap_peers_retrieve(struct BootstrapPeers* list) { +int repo_config_bootstrap_peers_retrieve(struct Libp2pVector** list) { char* default_bootstrap_addresses[] = { "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io @@ -17,29 +17,16 @@ int repo_config_bootstrap_peers_retrieve(struct BootstrapPeers* list) { "/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3", // mercury.i.ipfs.io "/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", // jupiter.i.ipfs.io }; + *list = libp2p_utils_vector_new(9); - list->num_peers = 9; - // allocate memory for list - list->peers = malloc(sizeof(struct IPFSAddr*) * list->num_peers); - if (list->peers == NULL) - return 0; - - for(int i = 0; i < list->num_peers; i++) { - struct IPFSAddr* currAddr; - if (ipfsaddr_new(&currAddr, default_bootstrap_addresses[i]) == 0) - return 0; - list->peers[i] = currAddr; + for(int i = 0; i < 9; i++) { + struct MultiAddress* currAddr = multiaddress_new_from_string(default_bootstrap_addresses[i]); + libp2p_utils_vector_add(*list, currAddr); } return 1; } -int repo_config_bootstrap_peers_free(struct BootstrapPeers* list) { - - for(int i = 0; i < list->num_peers; i++) { - if (list->peers[i] != NULL) { - ipfsaddr_free(list->peers[i]); - } - } - free(list->peers); +int repo_config_bootstrap_peers_free(struct Libp2pVector* list) { + libp2p_utils_vector_free(list); return 1; } diff --git a/repo/config/config.c b/repo/config/config.c index c6fa1c9..44100a4 100644 --- a/repo/config/config.c +++ b/repo/config/config.c @@ -87,7 +87,7 @@ int ipfs_repo_config_init(struct RepoConfig* config, unsigned int num_bits_for_k return 0; // bootstrap peers - retVal = repo_config_bootstrap_peers_retrieve(&(config->peer_addresses)); + retVal = repo_config_bootstrap_peers_retrieve(&(config->bootstrap_peers)); if (retVal == 0) return 0; @@ -142,8 +142,7 @@ int ipfs_repo_config_new(struct RepoConfig** config) { return 0; // set initial values - (*config)->peer_addresses.num_peers = 0; - (*config)->peer_addresses.peers = NULL; + (*config)->bootstrap_peers = NULL; int retVal = 1; retVal = repo_config_identity_new(&((*config)->identity)); @@ -174,8 +173,8 @@ int ipfs_repo_config_free(struct RepoConfig* config) { if (config != NULL) { if (config->identity != NULL) repo_config_identity_free(config->identity); - if (&(config->peer_addresses) != NULL) - repo_config_bootstrap_peers_free(&(config->peer_addresses)); + if (&(config->bootstrap_peers) != NULL) + repo_config_bootstrap_peers_free(config->bootstrap_peers); if (config->datastore != NULL) ipfs_repo_config_datastore_free(config->datastore); if (config->addresses != NULL) diff --git a/repo/fsrepo/fs_repo.c b/repo/fsrepo/fs_repo.c index a9f1a98..9c63008 100644 --- a/repo/fsrepo/fs_repo.c +++ b/repo/fsrepo/fs_repo.c @@ -443,6 +443,9 @@ int fs_repo_open_config(struct FSRepo* repo) { _get_json_string_value(data, tokens, num_tokens, curr_pos, "API", &repo->config->addresses->api); _get_json_string_value(data, tokens, num_tokens, curr_pos, "Gateway", &repo->config->addresses->gateway); + // bootstrap peers + repo->config->bootstrap_peers = libp2p_utils_vector_new(1); + // TODO: Implement bootstrap peers // free the memory used reading the json file free(data); free(priv_key_base64); diff --git a/routing/k_routing.c b/routing/k_routing.c index 7555bfd..9acdc6d 100644 --- a/routing/k_routing.c +++ b/routing/k_routing.c @@ -50,8 +50,6 @@ int ipfs_routing_kademlia_find_providers(struct s_ipfs_routing* routing, char* k *results = libp2p_utils_vector_new(1); struct Libp2pVector* vector = *results; // see if I can provide it - // temporarily commented out for testing... - /* unsigned char* peer_id = NULL; int peer_id_size = 0; if (libp2p_providerstore_get(routing->local_node->providerstore, (unsigned char*)key, key_size, &peer_id, &peer_id_size)) { @@ -65,7 +63,6 @@ int ipfs_routing_kademlia_find_providers(struct s_ipfs_routing* routing, char* k current = current->next; } } - */ //get a list of providers that are closest if (vector->total == 0) { // search requires null terminated key @@ -152,7 +149,7 @@ struct s_ipfs_routing* ipfs_routing_new_kademlia(struct IpfsNode* local_node, st // connect to nodes and listen for connections struct MultiAddress* address = multiaddress_new_from_string(local_node->repo->config->addresses->api); if (multiaddress_is_ip(address)) { - start_kademlia_multiaddress(address, kademlia_id, 10); + start_kademlia_multiaddress(address, kademlia_id, 10, local_node->repo->config->bootstrap_peers); } local_node->routing = routing; return routing; diff --git a/test/repo/test_repo_bootstrap_peers.h b/test/repo/test_repo_bootstrap_peers.h index e2ee221..4842253 100644 --- a/test/repo/test_repo_bootstrap_peers.h +++ b/test/repo/test_repo_bootstrap_peers.h @@ -19,19 +19,20 @@ int test_repo_bootstrap_peers_init() { "/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", // jupiter.i.ipfs.io }; - struct BootstrapPeers list; + struct Libp2pVector* list; int retVal = 1; repo_config_bootstrap_peers_retrieve(&list); - if ( list.num_peers != 9) { + if ( list->total != 9) { printf("Size does not equal 9 in test_repo_bootstrap_peers_init"); retVal = 0; } - for(int i = 0; i < list.num_peers; i++) { + for(int i = 0; i < list->total; i++) { unsigned long strLen = strlen(default_bootstrap_addresses[i]); - if (strncmp(list.peers[i]->entire_string, default_bootstrap_addresses[i], strLen) != 0) - printf("The value of element %d is: %s\n", i, list.peers[i]->entire_string); + struct MultiAddress* currAddr = libp2p_utils_vector_get(list, i); + if (strncmp(currAddr->string, default_bootstrap_addresses[i], strLen) != 0) + printf("The value of element %d is: %s\n", i, currAddr->string); } - repo_config_bootstrap_peers_free(&list); + repo_config_bootstrap_peers_free(list); return retVal; } diff --git a/test/routing/test_supernode.h b/test/routing/test_supernode.h index 4861428..ae43b57 100644 --- a/test/routing/test_supernode.h +++ b/test/routing/test_supernode.h @@ -87,7 +87,10 @@ int test_routing_supernode_get_remote_value() { 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); // set a different port for the dht/kademlia stuff - ipfs_node->repo->config->addresses->api = "/ip4/127.0.0.1/tcp/5002"; + strcpy(ipfs_node->repo->config->addresses->api, "/ip4/127.0.0.1/udp/5002"); + // add bootstrap peer for kademlia + struct MultiAddress* remote = multiaddress_new_from_string("/ip4/127.0.0.1/udp/5001"); + libp2p_utils_vector_add(ipfs_node->repo->config->bootstrap_peers, remote); ipfs_node->routing = ipfs_routing_new_kademlia(ipfs_node, &fs_repo->config->identity->private_key, stream);