c-libp2p/include/libp2p/conn/dialer.h

80 lines
2.9 KiB
C
Raw Normal View History

#pragma once
2017-02-13 13:47:55 +00:00
/***
* A local dialer. Uses MultiAddr to figure out the best way to
2017-02-13 18:26:41 +00:00
* connect to a client, then returns an open Connection that can be
2017-03-02 21:14:52 +00:00
* closed, read from and written to. The normal procedure is as follows:
* 1) Create a Dialer struct, with the required information about the local host
* 2) Call _get_connection to create the connection with a specific host
* NOTE: 1 Dialer could be used to create all connections, saving time and resources
* if it is not too difficult to pass it around. The struct has enough information to
* build connections that negotiate many protocols
2017-02-13 13:47:55 +00:00
*/
#include "libp2p/crypto/key.h"
2017-02-13 18:26:41 +00:00
#include "multiaddr/multiaddr.h"
2017-02-13 22:41:31 +00:00
#include "libp2p/conn/connection.h"
#include "libp2p/conn/transport_dialer.h"
2017-10-23 21:21:03 +00:00
#include "libp2p/peer/peer.h"
2017-02-13 13:47:55 +00:00
struct Dialer {
/**
* These two are used to create connections
*/
char* peer_id; // the local peer ID as null terminated string
2017-10-23 21:21:03 +00:00
struct RsaPrivateKey* private_key; // used to initiate secure connections, can be NULL, and connections will not be secured
2017-10-25 17:28:53 +00:00
struct Peerstore* peerstore; // used by secio to add peers to the collection
2017-02-13 13:47:55 +00:00
/**
* A linked list of transport dialers. A transport dialer can be selected
* based on the MultiAddr being dialed. Most common: TCP and UDP
*/
2017-02-13 18:26:41 +00:00
struct Libp2pLinkedList* transport_dialers;
2017-02-13 13:47:55 +00:00
//TODO: See dial.go, need to implement Protector
2017-02-13 18:26:41 +00:00
struct TransportDialer* fallback_dialer; // the default dialer. NOTE: this should not be in the list of transport_dialers
2017-02-13 13:47:55 +00:00
};
2017-02-13 18:26:41 +00:00
/**
* Create a Dialer with the specified local information
2017-03-02 21:14:52 +00:00
* NOTE: This fills in the fallback_dialer too
* @param peer_id the local PeerID
* @param private_key the local private key
* @returns a new Dialer struct
2017-02-13 18:26:41 +00:00
*/
2017-10-25 17:43:27 +00:00
struct Dialer* libp2p_conn_dialer_new(struct Libp2pPeer* peer, struct Peerstore* peerstore, struct RsaPrivateKey* private_key);
2017-02-13 18:26:41 +00:00
/**
2017-03-02 21:14:52 +00:00
* free resources from the Dialer struct
* @param in the Dialer struct to relieve of their resources
2017-02-13 18:26:41 +00:00
*/
void libp2p_conn_dialer_free(struct Dialer* in);
2017-10-23 21:21:03 +00:00
/***
* Attempt to connect to a particular peer
* @param dialer the dialer
* @param peer the peer to join
* @param timeout_secs network timeout in seconds
* @returns true(1) on success, false(0) otherwise
*/
int libp2p_conn_dialer_join_swarm(const struct Dialer* dialer, struct Libp2pPeer* peer, int timeout_secs);
2017-02-13 18:26:41 +00:00
/**
* Retrieve a Connection struct from the dialer
* @param dialer the dialer to use
* @param muiltiaddress who to connect to
* @returns a Connection, or NULL
*/
2017-10-23 23:03:38 +00:00
struct Stream* libp2p_conn_dialer_get_connection(const struct Dialer* dialer, const struct MultiAddress* multiaddress);
2017-03-02 21:14:52 +00:00
/**
* return a Stream that is already set up to use the passed in protocol
* @param dialer the dialer to use
2017-10-23 23:03:38 +00:00
* @param peer the host
2017-03-02 21:14:52 +00:00
* @param protocol the protocol to use (right now only 'multistream' is supported)
* @returns the ready-to-use stream
*/
2017-10-23 23:03:38 +00:00
struct Stream* libp2p_conn_dialer_get_stream(const struct Dialer* dialer, const struct Libp2pPeer* peer, const char* protocol);
2017-03-02 21:14:52 +00:00