c-libp2p/peer/peer.c

377 lines
11 KiB
C
Raw Permalink Normal View History

2017-02-20 23:53:20 +00:00
#include <stdlib.h>
#include <time.h>
2017-02-20 23:53:20 +00:00
#include "multiaddr/multiaddr.h"
#include "protobuf.h"
2017-04-03 22:26:08 +00:00
#include "libp2p/net/multistream.h"
2017-07-31 11:42:42 +00:00
#include "libp2p/peer/peer.h"
#include "libp2p/secio/secio.h"
#include "libp2p/utils/linked_list.h"
#include "libp2p/utils/logger.h"
2017-10-12 15:12:22 +00:00
#include "libp2p/yamux/yamux.h"
2017-10-23 21:21:03 +00:00
#include "libp2p/conn/dialer.h"
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() {
struct Libp2pPeer* out = (struct Libp2pPeer*)malloc(sizeof(struct Libp2pPeer));
if (out != NULL) {
out->id = NULL;
out->id_size = 0;
out->addr_head = NULL;
out->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
out->sessionContext = NULL;
out->is_local = 0;
2017-02-20 23:53:20 +00:00
}
return out;
}
/**
* Create a new Peer based on a multiaddress
* @param in the multiaddress
* @returns a Peer initialized with the values from "in"
*/
struct Libp2pPeer* libp2p_peer_new_from_multiaddress(const struct MultiAddress* in) {
struct Libp2pPeer* out = libp2p_peer_new();
char* id = multiaddress_get_peer_id(in);
if (id != NULL) {
2017-08-30 16:09:28 +00:00
out->id_size = strlen(id);
out->id = malloc(out->id_size);
2017-08-30 16:09:28 +00:00
memcpy(out->id, id, out->id_size);
2017-04-17 16:57:37 +00:00
free(id);
}
out->addr_head = libp2p_utils_linked_list_new();
out->addr_head->item = multiaddress_copy(in);
return out;
}
2017-07-27 19:32:42 +00:00
/***
* Free resources of a Libp2pPeer
* @param in the struct to free
*/
void libp2p_peer_free(struct Libp2pPeer* in) {
if (in != NULL) {
if (in->addr_head != NULL && in->addr_head->item != NULL) {
2017-08-30 16:09:28 +00:00
//libp2p_logger_debug("peer", "Freeing peer %s\n", ((struct MultiAddress*)in->addr_head->item)->string);
2017-07-27 19:32:42 +00:00
} else {
2017-08-30 16:09:28 +00:00
//libp2p_logger_debug("peer", "Freeing peer with no multiaddress.\n");
2017-07-27 19:32:42 +00:00
}
if (in->id != NULL)
free(in->id);
if (in->sessionContext != NULL) {
libp2p_session_context_free(in->sessionContext);
//libp2p_net_multistream_stream_free(in->connection);
in->sessionContext = NULL;
}
// free the memory in the linked list
struct Libp2pLinkedList* current = in->addr_head;
while (current != NULL) {
struct Libp2pLinkedList* temp = current->next;
multiaddress_free((struct MultiAddress*)current->item);
free(current);
current = temp;
}
free(in);
}
}
/***
* Clean up a bad connection
* @param peer the peer to clean up
* @returns true(1)
*/
int libp2p_peer_handle_connection_error(struct Libp2pPeer* peer) {
peer->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
libp2p_session_context_free(peer->sessionContext);
peer->sessionContext = NULL;
return 1;
}
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
*
* @param privateKey the local private key to use
2017-04-03 22:26:08 +00:00
* @param peer the peer to connect to
* @param peerstore if connection is successfull, will add peer to peerstore
2017-04-03 22:26:08 +00:00
* @returns true(1) on success, false(0) if we could not connect
*/
int libp2p_peer_connect(const struct Dialer* dialer, struct Libp2pPeer* peer, struct Peerstore* peerstore, struct Datastore *datastore, int timeout) {
// fix the connection type if in an invalid state
if (peer->connection_type == CONNECTION_TYPE_CONNECTED && peer->sessionContext == NULL)
peer->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
2017-08-30 16:09:28 +00:00
libp2p_logger_debug("peer", "Attemping to connect to %s.\n", libp2p_peer_id_to_string(peer));
2017-10-23 21:21:03 +00:00
return libp2p_conn_dialer_join_swarm(dialer, peer, timeout);
2017-04-03 22:26:08 +00:00
}
2017-02-20 23:53:20 +00:00
/**
* Make a copy of a peer
*
* NOTE: SessionContext is not copied.
*
2017-02-20 23:53:20 +00:00
* @param in what is to be copied
* @returns a new struct, that does not rely on the old
*/
struct Libp2pPeer* libp2p_peer_copy(const struct Libp2pPeer* in) {
2017-02-20 23:53:20 +00:00
struct Libp2pPeer* out = libp2p_peer_new();
if (out != NULL) {
out->is_local = in->is_local;
2017-02-20 23:53:20 +00:00
out->id_size = in->id_size;
out->id = malloc(in->id_size);
if (out->id == NULL) {
libp2p_peer_free(out);
return NULL;
}
memcpy(out->id, in->id, in->id_size);
out->connection_type = in->connection_type;
2017-04-06 01:34:13 +00:00
// loop through the addresses
2017-02-20 23:53:20 +00:00
struct Libp2pLinkedList* current_in = in->addr_head;
struct Libp2pLinkedList* current_out = NULL;
while (current_in != NULL) {
struct MultiAddress* addr = (struct MultiAddress*)current_in->item;
2017-09-28 18:21:07 +00:00
if (addr == NULL) {
libp2p_logger_error("peer", "Attempted to copy MultiAddress, but current item is NULL.\n");
2017-02-20 23:53:20 +00:00
} else {
2017-09-28 18:21:07 +00:00
struct Libp2pLinkedList* copy_item = libp2p_utils_linked_list_new();
copy_item->item = multiaddress_copy(addr);
if (out->addr_head == NULL) {
2017-09-28 21:31:35 +00:00
// first one
2017-09-28 18:21:07 +00:00
out->addr_head = copy_item;
} else {
2017-09-28 21:31:35 +00:00
// not the first, tack it on the end
2017-09-28 18:21:07 +00:00
current_out->next = copy_item;
}
current_out = copy_item;
2017-02-20 23:53:20 +00:00
}
current_in = current_in->next;
}
out->sessionContext = in->sessionContext;
2017-02-20 23:53:20 +00:00
}
return out;
}
/***
* Determine if the passed in peer and id match
* @param in the peer to check
* @param peer_id peer id, zero terminated string
* @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) {
if (peer_size == in->id_size) {
2017-07-17 21:14:20 +00:00
if (strncmp(in->id, (char*)peer_id, in->id_size) == 0)
return 1;
}
return 0;
}
2017-08-30 16:09:28 +00:00
static char string_retval[100];
/***
* 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-08-30 16:09:28 +00:00
memcpy(string_retval, in->id, in->id_size);
string_retval[in->id_size] = 0;
return string_retval;
}
/***
* 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) {
if (in->connection_type == CONNECTION_TYPE_CONNECTED) {
if (in->sessionContext == NULL || in->sessionContext->default_stream == NULL) {
in->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
}
}
return in->connection_type == CONNECTION_TYPE_CONNECTED;
}
2017-02-20 23:53:20 +00:00
size_t libp2p_peer_protobuf_encode_size(struct Libp2pPeer* in) {
int sz = 0;
if (in != NULL) {
// id + connection_type
sz = 11 + in->id_size + 11;
// loop through the multiaddresses
struct Libp2pLinkedList* current = in->addr_head;
while (current != NULL) {
// find the length of the MultiAddress converted into bytes
struct MultiAddress* data = (struct MultiAddress*)current->item;
2017-09-28 18:21:07 +00:00
if (data == NULL) {
libp2p_logger_error("peer", "encode_size: Attempted to get MultiAddress, but item was NULL.\n");
} else {
sz += 11 + data->bsize;
}
current = current->next;
}
2017-02-20 23:53:20 +00:00
}
return sz;
}
int libp2p_peer_protobuf_encode(struct Libp2pPeer* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written) {
// data & data_size
size_t bytes_used = 0;
*bytes_written = 0;
int retVal = 0;
// field 1 (id)
retVal = protobuf_encode_length_delimited(1, WIRETYPE_LENGTH_DELIMITED, in->id, in->id_size, &buffer[*bytes_written], max_buffer_size - *bytes_written, &bytes_used);
if (retVal == 0)
return 0;
*bytes_written += bytes_used;
// field 2 (repeated)
struct Libp2pLinkedList* current = in->addr_head;
while (current != NULL) {
struct MultiAddress* data = (struct MultiAddress*)current->item;
2017-09-28 18:21:07 +00:00
if (data == NULL) {
libp2p_logger_error("peer", "encode: Attempted to get multiaddress, but item was NULL.\n");
} else {
retVal = protobuf_encode_length_delimited(2, WIRETYPE_LENGTH_DELIMITED, (char*)data->bytes, data->bsize, &buffer[*bytes_written], max_buffer_size - *bytes_written, &bytes_used);
if (retVal == 0)
return 0;
*bytes_written += bytes_used;
}
2017-02-20 23:53:20 +00:00
current = current->next;
}
// field 3 (varint)
retVal = protobuf_encode_varint(3, WIRETYPE_VARINT, in->connection_type, &buffer[*bytes_written], max_buffer_size - *bytes_written, &bytes_used);
if (retVal == 0)
return 0;
*bytes_written += bytes_used;
return 1;
}
int libp2p_peer_protobuf_encode_with_alloc(struct Libp2pPeer* in, unsigned char** buffer, size_t *buffer_size) {
*buffer_size = libp2p_peer_protobuf_encode_size(in);
*buffer = malloc(*buffer_size);
return libp2p_peer_protobuf_encode(in, *buffer, *buffer_size, buffer_size);
}
2017-02-20 23:53:20 +00:00
int libp2p_peer_protobuf_decode(unsigned char* in, size_t in_size, struct Libp2pPeer** out) {
size_t pos = 0;
int retVal = 0;
char* buffer = NULL;
size_t buffer_size = 0;
struct Libp2pLinkedList* current = NULL;
struct Libp2pLinkedList* last = NULL;
struct MultiAddress* ma = NULL;
*out = libp2p_peer_new();
if ( *out == NULL)
2017-02-20 23:53:20 +00:00
goto exit;
struct Libp2pPeer* ptr = *out;
ptr->addr_head = NULL;
while(pos < in_size) {
size_t bytes_read = 0;
int field_no;
enum WireType field_type;
if (protobuf_decode_field_and_type(&in[pos], in_size, &field_no, &field_type, &bytes_read) == 0) {
goto exit;
}
pos += bytes_read;
switch(field_no) {
case (1): // id
if (!protobuf_decode_length_delimited(&in[pos], in_size - pos, (char**)&(ptr->id),&(ptr->id_size), &bytes_read))
goto exit;
pos += bytes_read;
break;
case (2): { // multiaddress bytes
if (!protobuf_decode_length_delimited(&in[pos], in_size - pos, &buffer, &buffer_size, &bytes_read))
goto exit;
pos += bytes_read;
// now turn it into multiaddress
struct Libp2pLinkedList* current = libp2p_utils_linked_list_new();
if (current == NULL)
goto exit;
2017-07-17 21:14:20 +00:00
struct MultiAddress* address = multiaddress_new_from_bytes((unsigned char*)buffer, buffer_size);
current->item = (void*)address;
2017-02-20 23:53:20 +00:00
free(buffer);
buffer = NULL;
// assign the values
if (ptr->addr_head == NULL) {
ptr->addr_head = current;
} else {
last->next = current;
}
last = current;
current = NULL;
break;
}
case (3): // enum as varint
if (!protobuf_decode_varint(&in[pos], in_size - pos, (long long unsigned int*)&ptr->connection_type, &bytes_read))
goto exit;
pos += bytes_read;
break;
}
}
retVal = 1;
exit:
if (retVal == 0) {
free(*out);
*out = NULL;
}
if (buffer != NULL)
free(buffer);
return retVal;
}
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) {
if (a == NULL && b == NULL)
return 0;
if (a == NULL && b != NULL)
return -1;
if (a != NULL && b == NULL)
return 1;
if (a->id_size != b->id_size)
return b->id_size - a->id_size;
for(int i = 0; i < a->id_size; i++) {
if (a->id[i] != b->id[i])
return b->id[i] - a->id[i];
}
return 0;
}
2017-10-25 17:28:53 +00:00
2017-11-27 16:10:38 +00:00
struct Stream* libp2p_peer_get_connection_stream(struct Stream* incoming_stream) {
struct Stream* current_stream = incoming_stream;
while (current_stream->parent_stream != NULL) {
current_stream = current_stream->parent_stream;
}
if (current_stream != NULL && current_stream->stream_type == STREAM_TYPE_RAW)
return current_stream;
return NULL;
}
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) {
unsigned long long retVal = 0;
if (peer != NULL) {
if (peer->sessionContext != NULL) {
2017-11-27 16:10:38 +00:00
struct Stream* connectionStream = libp2p_peer_get_connection_stream(peer->sessionContext->default_stream);
if (connectionStream != NULL) {
struct ConnectionContext* ctx = (struct ConnectionContext*)connectionStream->stream_context;
2017-10-25 17:28:53 +00:00
retVal = ctx->last_comm_epoch;
}
}
}
return retVal;
}