c-libp2p/include/libp2p/routing/dht_protocol.h

77 lines
2.5 KiB
C
Raw Permalink Normal View History

2017-03-30 18:58:53 +00:00
#pragma once
#include "libp2p/conn/session.h"
2017-09-14 21:49:50 +00:00
#include "libp2p/net/protocol.h"
2017-03-30 18:58:53 +00:00
#include "libp2p/peer/peerstore.h"
#include "libp2p/peer/providerstore.h"
2017-09-14 21:49:50 +00:00
#include "libp2p/record/message.h"
2017-03-30 18:58:53 +00:00
/***
* This is where kademlia and dht talk to the outside world
*/
struct DhtContext {
struct Peerstore* peer_store;
struct ProviderStore* provider_store;
struct Datastore* datastore;
struct Filestore* filestore;
};
struct Libp2pProtocolHandler* libp2p_routing_dht_build_protocol_handler(struct Peerstore* peer_store, struct ProviderStore* provider_store,
struct Datastore* datastore, struct Filestore* filestore);
2017-03-30 18:58:53 +00:00
/**
* Take existing stream and upgrade to the Kademlia / DHT protocol/codec
* @param context the context
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_routing_dht_upgrade_stream(struct SessionContext* context);
/**
* Handle a client requesting an upgrade to the DHT protocol
* @param stream the stream
2017-03-30 18:58:53 +00:00
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_routing_dht_handshake(struct Stream* stream);
2017-03-30 18:58:53 +00:00
/***
* Handle the incoming message. Handshake should have already
* been done. We should expect that the next read contains
* a protobuf'd kademlia message.
* @param stream the incoming stream
* @param protocol_context the protocol context
2017-03-30 18:58:53 +00:00
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_routing_dht_handle_message(struct Stream* stream, struct DhtContext* protocol_context);
2017-09-14 21:49:50 +00:00
/***
* Send a kademlia message
* NOTE: this call upgrades the stream to /ipfs/kad/1.0.0
* @param context the context
* @param message the message
* @returns true(1) on success, false(0) otherwise
*/
int libp2p_routing_dht_send_message(struct SessionContext* sessionContext, struct KademliaMessage* message);
/**
* Attempt to receive a kademlia message
* NOTE: This call assumes that a send_message was sent
* @param sessionContext the context
* @param result where to put the results
* @returns true(1) on success, false(0) otherwise
*/
int libp2p_routing_dht_receive_message(struct SessionContext* sessionContext, struct KademliaMessage** result);
2017-09-14 21:49:50 +00:00
/**
* Used to send a message to the nearest x peers
*
* @param private_key the private key of the local peer
* @param peerstore the collection of peers
* @param datastore a connection to the datastore
2017-09-14 21:49:50 +00:00
* @param msg the message to send
* @returns true(1) if we sent to at least 1, false(0) otherwise
2017-09-14 21:49:50 +00:00
*/
int libp2p_routing_dht_send_message_nearest_x(const struct Dialer* dialer, struct Peerstore* peerstore,
struct Datastore* datastore, struct KademliaMessage* msg, int numToSend);