diff --git a/core/null.c b/core/null.c index 01800d5..2b99680 100644 --- a/core/null.c +++ b/core/null.c @@ -55,7 +55,7 @@ int protocol_compare(const unsigned char* incoming, size_t incoming_size, const int ipfs_null_marshal(const unsigned char* incoming, size_t incoming_size, struct SessionContext* session, struct null_connection_params *connection_param) { if (protocol_compare(incoming, incoming_size, "/secio")) { libp2p_logger_debug("null", "Attempting secure io connection...\n"); - if (!libp2p_secio_handshake(session, &connection_param->local_node->identity->private_key, 1)) { + if (!libp2p_secio_handshake(session, &connection_param->local_node->identity->private_key, connection_param->local_node->peerstore, 1)) { // rejecting connection libp2p_logger_debug("null", "Secure IO connection failed\n"); return 0; diff --git a/exchange/bitswap/network.c b/exchange/bitswap/network.c index f7e3706..fd799d7 100644 --- a/exchange/bitswap/network.c +++ b/exchange/bitswap/network.c @@ -17,7 +17,7 @@ int ipfs_bitswap_network_send_message(const struct BitswapContext* context, struct Libp2pPeer* peer, const struct BitswapMessage* message) { // get a connection to the peer if (peer->connection_type != CONNECTION_TYPE_CONNECTED) { - libp2p_peer_connect(&context->ipfsNode->identity->private_key, peer, 10); + libp2p_peer_connect(&context->ipfsNode->identity->private_key, peer, context->ipfsNode->peerstore, 10); if(peer->connection_type != CONNECTION_TYPE_CONNECTED) return 0; } @@ -78,8 +78,36 @@ int ipfs_bitswap_network_handle_message(const struct IpfsNode* node, const struc // wantlist - what they want if (message->wantlist != NULL && message->wantlist->entries != NULL && message->wantlist->entries->total > 0) { // get the peer - struct Libp2pPeer* peer = libp2p_peerstore_get_peer(node->peerstore, (unsigned char*)sessionContext->remote_peer_id, strlen(sessionContext->remote_peer_id)); + if (sessionContext->remote_peer_id == NULL) { + ipfs_bitswap_message_free(message); + return 0; + } + struct Libp2pPeer* temp_peer = libp2p_peer_new(); + temp_peer->id_size = strlen(sessionContext->remote_peer_id); + temp_peer->id = malloc(temp_peer->id_size); + if (temp_peer->id == NULL) { + libp2p_peer_free(temp_peer); + ipfs_bitswap_message_free(message); + return 0; + } + memcpy(temp_peer->id, sessionContext->remote_peer_id, strlen(sessionContext->remote_peer_id)); + struct Libp2pPeer* peer = libp2p_peerstore_get_or_add_peer(node->peerstore, temp_peer); + libp2p_peer_free(temp_peer); + if (peer == NULL) { + ipfs_bitswap_message_free(message); + return 0; + } struct PeerRequestEntry* queueEntry = ipfs_bitswap_peer_request_queue_find_entry(bitswapContext->peerRequestQueue, peer); + if (queueEntry == NULL) { + struct PeerRequest* peerRequest =ipfs_bitswap_peer_request_new(); + peerRequest->peer = peer; + ipfs_bitswap_peer_request_queue_add(bitswapContext->peerRequestQueue, peerRequest); + queueEntry = ipfs_bitswap_peer_request_queue_find_entry(bitswapContext->peerRequestQueue, peer); + if (queueEntry == NULL) { + ipfs_bitswap_message_free(message); + return 0; + } + } for(int i = 0; i < message->wantlist->entries->total; i++) { struct WantlistEntry* entry = (struct WantlistEntry*) libp2p_utils_vector_get(message->wantlist->entries, i); // turn the "block" back into a cid diff --git a/exchange/bitswap/peer_request_queue.c b/exchange/bitswap/peer_request_queue.c index 73a388d..6e56d54 100644 --- a/exchange/bitswap/peer_request_queue.c +++ b/exchange/bitswap/peer_request_queue.c @@ -215,7 +215,7 @@ int ipfs_bitswap_peer_request_process_entry(const struct BitswapContext* context if (need_to_connect) { if (!connected) { // connect - connected = libp2p_peer_connect(&context->ipfsNode->identity->private_key, request->peer, 0); + connected = libp2p_peer_connect(&context->ipfsNode->identity->private_key, request->peer, context->ipfsNode->peerstore, 0); } if (connected) { // build a message diff --git a/routing/online.c b/routing/online.c index f664fd5..47ca6aa 100644 --- a/routing/online.c +++ b/routing/online.c @@ -294,7 +294,7 @@ int ipfs_routing_online_ping(struct IpfsRouting* routing, struct Libp2pPeer* pee size_t protobuf_size; if (peer->connection_type != CONNECTION_TYPE_CONNECTED) { - if (!libp2p_peer_connect(&routing->local_node->identity->private_key, peer, 5)) + if (!libp2p_peer_connect(&routing->local_node->identity->private_key, peer, routing->local_node->peerstore, 5)) return 0; } if (peer->connection_type == CONNECTION_TYPE_CONNECTED) { @@ -414,7 +414,7 @@ int ipfs_routing_online_get_value (ipfs_routing* routing, const unsigned char *k if (!libp2p_peer_is_connected(current_peer)) { // attempt to connect. If unsuccessful, continue in the loop. libp2p_logger_debug("online", "Attempting to connect to peer to retrieve file\n"); - if (libp2p_peer_connect(&routing->local_node->identity->private_key, current_peer, 5)) { + if (libp2p_peer_connect(&routing->local_node->identity->private_key, current_peer, routing->local_node->peerstore, 5)) { libp2p_logger_debug("online", "Peer connected\n"); if (ipfs_routing_online_get_peer_value(routing, current_peer, key, key_size, buffer, buffer_size)) { libp2p_logger_debug("online", "Retrieved a value\n"); @@ -487,7 +487,7 @@ int ipfs_routing_online_bootstrap(struct IpfsRouting* routing) { return -1; // this should never happen } if (peer->sessionContext == NULL) { // should always be true unless we added it twice (TODO: we should prevent that earlier) - libp2p_peer_connect(&routing->local_node->identity->private_key, peer, 5); + libp2p_peer_connect(&routing->local_node->identity->private_key, peer, routing->local_node->peerstore, 5); } } } diff --git a/test/routing/test_routing.h b/test/routing/test_routing.h index 7ec9185..02afcf1 100644 --- a/test/routing/test_routing.h +++ b/test/routing/test_routing.h @@ -233,7 +233,7 @@ int test_routing_find_providers() { struct Libp2pPeer *remote_peer = NULL; for(int i = 0; i < result->total; i++) { remote_peer = (struct Libp2pPeer*)libp2p_utils_vector_get(result, i); - if (remote_peer->connection_type == CONNECTION_TYPE_CONNECTED || libp2p_peer_connect(&local_node.identity->private_key, remote_peer, 5)) { + if (remote_peer->connection_type == CONNECTION_TYPE_CONNECTED || libp2p_peer_connect(&local_node.identity->private_key, remote_peer, local_node.peerstore, 5)) { break; } remote_peer = NULL;