2017-02-20 23:53:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-27 17:27:14 +00:00
|
|
|
#include "multiaddr/multiaddr.h"
|
2017-04-03 22:26:08 +00:00
|
|
|
#include "libp2p/net/stream.h"
|
2017-07-31 11:42:42 +00:00
|
|
|
#include "libp2p/crypto/rsa.h"
|
2017-08-02 13:52:55 +00:00
|
|
|
#include "libp2p/conn/session.h"
|
2017-10-23 21:21:03 +00:00
|
|
|
//#include "libp2p/conn/dialer.h"
|
2017-02-27 17:27:14 +00:00
|
|
|
|
2017-07-31 21:36:08 +00:00
|
|
|
struct Peerstore;
|
2017-10-23 21:21:03 +00:00
|
|
|
struct Dialer;
|
2017-07-31 21:36:08 +00:00
|
|
|
|
2017-02-20 23:53:20 +00:00
|
|
|
enum ConnectionType {
|
|
|
|
// sender does not have a connection to the peer, and no extra information (default)
|
|
|
|
CONNECTION_TYPE_NOT_CONNECTED = 0,
|
|
|
|
// sender has a live connection to the peer
|
|
|
|
CONNECTION_TYPE_CONNECTED = 1,
|
|
|
|
// sender recently connected to peer
|
|
|
|
CONNECTION_TYPE_CAN_CONNECT = 2,
|
|
|
|
// sender recently tried to connect to peer repeatedly but failed to connect
|
|
|
|
CONNECTION_TYPE_CANNOT_CONNECT = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Libp2pPeer {
|
2017-03-02 21:14:52 +00:00
|
|
|
char* id; // protobuf field 1; the ID (aka peer id) of the peer
|
|
|
|
size_t id_size; // the length of id
|
2017-02-20 23:53:20 +00:00
|
|
|
struct Libp2pLinkedList* addr_head; // protobuf field 2 of multiaddr bytes (repeatable) (stored here as a struct MultiAddr)
|
|
|
|
enum ConnectionType connection_type; // protobuf field 3 (a varint)
|
2017-07-27 17:06:27 +00:00
|
|
|
struct SessionContext *sessionContext; // not protobuf'd, the current connection to the peer
|
2017-07-31 17:49:41 +00:00
|
|
|
int is_local; // not protobuf'd, true if this is the local peer
|
2017-02-20 23:53:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a new Peer struct
|
|
|
|
* @returns a struct or NULL if there was a problem
|
|
|
|
*/
|
|
|
|
struct Libp2pPeer* libp2p_peer_new();
|
|
|
|
|
2017-02-27 17:27:14 +00:00
|
|
|
/**
|
2017-04-17 04:46:52 +00:00
|
|
|
* Create a new Peer based on a multiaddress
|
|
|
|
* @param in the multiaddress
|
|
|
|
* @returns a Peer initialized with the values from "in"
|
2017-02-27 17:27:14 +00:00
|
|
|
*/
|
2017-04-17 04:46:52 +00:00
|
|
|
struct Libp2pPeer* libp2p_peer_new_from_multiaddress(const struct MultiAddress* multi_addr);
|
2017-02-27 17:27:14 +00:00
|
|
|
|
2017-02-20 23:53:20 +00:00
|
|
|
/**
|
|
|
|
* frees resources from a peer struct
|
|
|
|
* @param in the peer to free
|
|
|
|
*/
|
|
|
|
void libp2p_peer_free(struct Libp2pPeer* in);
|
|
|
|
|
2017-04-03 22:26:08 +00:00
|
|
|
/**
|
|
|
|
* Attempt to connect to the peer, setting connection_type correctly
|
|
|
|
* NOTE: If successful, this will set peer->connection to the stream
|
2017-07-31 21:36:08 +00:00
|
|
|
*
|
2017-10-23 20:21:50 +00:00
|
|
|
* @param dialer the dialer
|
2017-04-03 22:26:08 +00:00
|
|
|
* @param peer the peer to connect to
|
2017-07-31 21:36:08 +00:00
|
|
|
* @param peerstore if connection is successfull, will add peer to peerstore
|
2017-08-28 11:55:38 +00:00
|
|
|
* @param timeout number of seconds before giving up
|
2017-04-03 22:26:08 +00:00
|
|
|
* @returns true(1) on success, false(0) if we could not connect
|
|
|
|
*/
|
2017-10-23 20:21:50 +00:00
|
|
|
int libp2p_peer_connect(const struct Dialer* dialer, struct Libp2pPeer* peer, struct Peerstore* peerstore, struct Datastore* datastore, int timeout);
|
2017-04-03 22:26:08 +00:00
|
|
|
|
2017-08-03 16:15:40 +00:00
|
|
|
/***
|
|
|
|
* Clean up a bad connection
|
|
|
|
* @param peer the peer to clean up
|
|
|
|
* @returns true(1)
|
|
|
|
*/
|
|
|
|
int libp2p_peer_handle_connection_error(struct Libp2pPeer* peer);
|
|
|
|
|
2017-02-20 23:53:20 +00:00
|
|
|
/**
|
|
|
|
* Make a copy of a peer
|
|
|
|
* @param in what is to be copied
|
|
|
|
* @returns a new struct, that does not rely on the old
|
|
|
|
*/
|
2017-07-26 12:37:28 +00:00
|
|
|
struct Libp2pPeer* libp2p_peer_copy(const struct Libp2pPeer* in);
|
2017-02-20 23:53:20 +00:00
|
|
|
|
2017-07-31 11:42:42 +00:00
|
|
|
/**
|
|
|
|
* Compare 2 Libp2pPeers
|
|
|
|
* @param a side A
|
|
|
|
* @param b side B
|
|
|
|
* @returns <0 if A wins, 0 if equal, or >0 if B wins
|
|
|
|
*/
|
|
|
|
int libp2p_peer_compare(const struct Libp2pPeer* a, const struct Libp2pPeer* b);
|
|
|
|
|
2017-04-20 22:55:18 +00:00
|
|
|
/***
|
|
|
|
* Determine if the passed in peer and id match
|
|
|
|
* @param in the peer to check
|
2017-08-30 16:09:28 +00:00
|
|
|
* @param peer_id peer id
|
|
|
|
* @param peer_size length of peer_id
|
2017-04-20 22:55:18 +00:00
|
|
|
* @returns true if peer matches
|
|
|
|
*/
|
2017-08-30 16:09:28 +00:00
|
|
|
int libp2p_peer_matches_id(struct Libp2pPeer* in, const unsigned char* peer_id, int peer_size);
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Convert peer id to null terminated string
|
|
|
|
* @param in the peer object
|
|
|
|
* @returns the peer id as a null terminated string
|
|
|
|
*/
|
2017-09-28 18:21:07 +00:00
|
|
|
char* libp2p_peer_id_to_string(const struct Libp2pPeer* in);
|
2017-04-20 22:55:18 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Determine if we are currently connected to this peer
|
|
|
|
* @param in the peer to check
|
|
|
|
* @returns true(1) if connected
|
|
|
|
*/
|
|
|
|
int libp2p_peer_is_connected(struct Libp2pPeer* in);
|
|
|
|
|
2017-02-20 23:53:20 +00:00
|
|
|
/**
|
|
|
|
* Get an estimate of the necessary size of the buffer to protobuf a particular peer
|
|
|
|
* @param in the peer to examine
|
|
|
|
* @returns an approximation of the buffer size required (erring on the side of bigger)
|
|
|
|
*/
|
|
|
|
size_t libp2p_peer_protobuf_encode_size(struct Libp2pPeer* in);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode the Peer into a buffer
|
|
|
|
* @param in the peer
|
|
|
|
* @param buffer where to put it
|
|
|
|
* @param max_buffer_size the maximum amount of memory reserved for the buffer
|
|
|
|
* @param bytes_written the number of bytes written to the buffer
|
|
|
|
* @returns true(1) on success, otherwise 0
|
|
|
|
*/
|
|
|
|
int libp2p_peer_protobuf_encode(struct Libp2pPeer* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written);
|
|
|
|
|
2017-04-17 04:46:52 +00:00
|
|
|
/**
|
|
|
|
* Encode the Peer into a buffer
|
|
|
|
* @param in the peer
|
|
|
|
* @param buffer where to put it (will be allocated by this function)
|
|
|
|
* @param buffer_size the number of bytes written to the buffer
|
|
|
|
* @returns true(1) on success, otherwise 0
|
|
|
|
*/
|
|
|
|
int libp2p_peer_protobuf_encode_with_alloc(struct Libp2pPeer* in, unsigned char** buffer, size_t *buffer_size);
|
|
|
|
|
2017-02-20 23:53:20 +00:00
|
|
|
/**
|
|
|
|
* turn an array of bytes into a Peer
|
|
|
|
* @param in the protobuf formatted peer
|
|
|
|
* @param in_size the size of in
|
|
|
|
* @param out the new Peer
|
|
|
|
* @returns true(1) on success, otherwise false
|
|
|
|
*/
|
|
|
|
int libp2p_peer_protobuf_decode(unsigned char* in, size_t in_size, struct Libp2pPeer** out);
|
|
|
|
|
2017-10-25 17:28:53 +00:00
|
|
|
/***
|
|
|
|
* Get the last time we communicated with this peer as an epoch
|
|
|
|
* @param peer the peer to examine
|
|
|
|
* @returns the last time we communicated with this peer (0 indicates never in this session, or disconnected)
|
|
|
|
*/
|
|
|
|
unsigned long long libp2p_peer_last_comm(const struct Libp2pPeer* peer);
|