2017-03-09 17:50:08 +00:00
|
|
|
|
|
|
|
#include "libp2p/peer/peer.h"
|
2017-04-03 22:26:33 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2017-03-09 23:47:27 +00:00
|
|
|
#include "ipfs/routing/routing.h"
|
2017-03-09 17:50:08 +00:00
|
|
|
#include "ipfs/core/ipfs_node.h"
|
|
|
|
#include "ipfs/thirdparty/ipfsaddr/ipfs_addr.h"
|
|
|
|
#include "multiaddr/multiaddr.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Begin to connect to the swarm
|
|
|
|
*/
|
|
|
|
void *ipfs_bootstrap_swarm(void* param) {
|
2017-03-24 21:51:00 +00:00
|
|
|
//TODO:
|
|
|
|
/*
|
2017-03-09 17:50:08 +00:00
|
|
|
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
|
|
|
|
struct IPFSAddr* ipfs_addr = local_node->repo->config->peer_addresses.peers[i];
|
|
|
|
struct MultiAddress* ma = multiaddress_new_from_string(ipfs_addr->entire_string);
|
|
|
|
// get the id
|
|
|
|
char* ptr;
|
|
|
|
if ( (ptr = strstr(ipfs_addr->entire_string, "/ipfs/")) != NULL) { // look for the peer id
|
|
|
|
ptr += 6;
|
|
|
|
if (ptr[0] == 'Q' && ptr[1] == 'm') { // things look good
|
|
|
|
struct Libp2pPeer* peer = libp2p_peer_new_from_data(ptr, strlen(ptr), ma);
|
|
|
|
libp2p_peerstore_add_peer(local_node->peerstore, peer);
|
|
|
|
}
|
|
|
|
// TODO: attempt to connect to the peer
|
|
|
|
|
|
|
|
} // we have a good peer ID
|
|
|
|
|
|
|
|
}
|
2017-03-24 21:51:00 +00:00
|
|
|
*/
|
2017-03-09 17:50:08 +00:00
|
|
|
return (void*)1;
|
|
|
|
}
|
2017-03-09 23:03:21 +00:00
|
|
|
|
2017-03-24 19:29:00 +00:00
|
|
|
/***
|
|
|
|
* Announce to the network all of the files that I have in storage
|
|
|
|
* @param local_node the context
|
|
|
|
*/
|
|
|
|
void ipfs_bootstrap_announce_files(struct IpfsNode* local_node) {
|
|
|
|
struct Datastore* db = local_node->repo->config->datastore;
|
|
|
|
if (!db->datastore_cursor_open(db))
|
|
|
|
return;
|
|
|
|
unsigned char* key = NULL;
|
|
|
|
int key_size = 0;
|
|
|
|
enum DatastoreCursorOp op = CURSOR_FIRST;
|
|
|
|
while (db->datastore_cursor_get(&key, &key_size, NULL, 0, op, db)) {
|
2017-04-03 22:26:33 +00:00
|
|
|
libp2p_logger_debug("bootstrap", "Announcing a file to the world.\n");
|
2017-04-20 22:56:03 +00:00
|
|
|
local_node->routing->Provide(local_node->routing, key, key_size);
|
2017-03-24 19:29:00 +00:00
|
|
|
op = CURSOR_NEXT;
|
|
|
|
free(key);
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:58:47 +00:00
|
|
|
// close cursor
|
|
|
|
db->datastore_cursor_close(db);
|
|
|
|
|
2017-03-24 19:29:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-09 23:03:21 +00:00
|
|
|
/***
|
2017-04-03 22:26:33 +00:00
|
|
|
* connect to the swarm
|
2017-03-09 23:03:21 +00:00
|
|
|
* NOTE: This fills in the IpfsNode->routing struct
|
|
|
|
*
|
|
|
|
* @param param the IpfsNode information
|
|
|
|
* @returns nothing useful
|
|
|
|
*/
|
|
|
|
void *ipfs_bootstrap_routing(void* param) {
|
|
|
|
struct IpfsNode* local_node = (struct IpfsNode*)param;
|
2017-04-03 22:26:33 +00:00
|
|
|
local_node->routing = ipfs_routing_new_online(local_node, &local_node->identity->private_key, NULL);
|
|
|
|
local_node->routing->Bootstrap(local_node->routing);
|
2017-03-24 19:29:00 +00:00
|
|
|
ipfs_bootstrap_announce_files(local_node);
|
2017-03-21 17:11:41 +00:00
|
|
|
return (void*)2;
|
2017-03-09 23:03:21 +00:00
|
|
|
}
|