Handling finding of peer via swarm

yamux
John Jones 2017-04-13 09:30:28 -05:00
parent dc546649fd
commit e886fe3288
7 changed files with 82 additions and 13 deletions

View File

@ -44,7 +44,7 @@ void libp2p_message_free(struct Libp2pMessage* in);
* @param in the struct to be protobuf'd
* @returns the size required
*/
size_t libp2p_message_protobuf_encode_size(struct Libp2pMessage* in);
size_t libp2p_message_protobuf_encode_size(const struct Libp2pMessage* in);
/**
* Encode a Message into a protobuf
@ -54,7 +54,17 @@ size_t libp2p_message_protobuf_encode_size(struct Libp2pMessage* in);
* @param bytes_written will hold the number of bytes written to buffer
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_message_protobuf_encode(struct Libp2pMessage* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written);
int libp2p_message_protobuf_encode(const struct Libp2pMessage* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written);
/**
* Convert a Libp2pMessage into protobuf format,
* allocating memory as needed
* @param in the Libp2pMessage to convert
* @param buffer where to store the protobuf
* @param buffer_size the size written into buffer
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_message_protobuf_allocate_and_encode(const struct Libp2pMessage* in, unsigned char **buffer, size_t* buffer_size);
/**
* turn a protobuf back into a message

View File

@ -43,6 +43,16 @@ void libp2p_record_free(struct Libp2pRecord* in);
*/
int libp2p_record_protobuf_encode(const struct Libp2pRecord* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written);
/**
* Convert a Libp2pRecord into protobuf format, allocating
* memory as needed
* @param in the Libp2pRecord to convert
* @param buffer where to store the protobuf
* @param buffer_size the size of the allocated buffer
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_record_protobuf_allocate_and_encode(const struct Libp2pRecord* in, unsigned char **buffer, size_t *buffer_size);
/**
* Generates an estimate of the buffer size needed to encode the struct
* @param in the Libp2pRecord that you want to encode

View File

@ -31,8 +31,8 @@ int libp2p_net_multistream_close(void* stream_context) {
* @returns the number of bytes written
*/
int libp2p_net_multistream_write(void* stream_context, const unsigned char* data, size_t data_length) {
struct SessionContext* secure_context = (struct SessionContext*)stream_context;
struct Stream* stream = secure_context->insecure_stream;
struct SessionContext* session_context = (struct SessionContext*)stream_context;
struct Stream* stream = session_context->insecure_stream;
int num_bytes = 0;
if (data_length > 0) { // only do this is if there is something to send
@ -58,8 +58,8 @@ int libp2p_net_multistream_write(void* stream_context, const unsigned char* data
* @returns number of bytes received
*/
int libp2p_net_multistream_read(void* stream_context, unsigned char** results, size_t* results_size) {
struct SessionContext* secure_context = (struct SessionContext*)stream_context;
struct Stream* stream = secure_context->insecure_stream;
struct SessionContext* session_context = (struct SessionContext*)stream_context;
struct Stream* stream = session_context->insecure_stream;
int bytes = 0;
size_t buffer_size = 65535;
char buffer[buffer_size];

View File

@ -128,11 +128,12 @@ struct PeerEntry* libp2p_peerstore_get_peer_entry(struct Peerstore* peerstore, c
struct Libp2pLinkedList* current = peerstore->head_entry;
while(current != NULL) {
struct Libp2pPeer* peer = ((struct PeerEntry*)current->item)->peer;
if (peer->id_size != peer_id_size)
continue;
if (memcmp(peer_id, peer->id, peer->id_size) == 0) {
return (struct PeerEntry*)current->item;
if (peer->id_size == peer_id_size) {
if (memcmp(peer_id, peer->id, peer->id_size) == 0) {
return (struct PeerEntry*)current->item;
}
}
current = current->next;
}
return NULL;
}

View File

@ -53,7 +53,7 @@ void libp2p_message_free(struct Libp2pMessage* in) {
}
}
size_t libp2p_message_protobuf_encode_size(struct Libp2pMessage* in) {
size_t libp2p_message_protobuf_encode_size(const struct Libp2pMessage* in) {
// message type
size_t retVal = 11;
// clusterlevelraw
@ -77,7 +77,31 @@ size_t libp2p_message_protobuf_encode_size(struct Libp2pMessage* in) {
return retVal;
}
int libp2p_message_protobuf_encode(struct Libp2pMessage* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written) {
/**
* Convert a Libp2pMessage into protobuf format,
* allocating memory as needed
* @param in the Libp2pMessage to convert
* @param buffer where to store the protobuf
* @param buffer_size the size of the buffer
* @returns true(1) on success, otherwise false(0)
*/
int libp2p_message_protobuf_allocate_and_encode(const struct Libp2pMessage* in, unsigned char **buffer, size_t *buffer_size) {
*buffer_size = libp2p_message_protobuf_encode_size(in);
*buffer = malloc(*buffer_size);
if (*buffer == NULL) {
*buffer_size = 0;
return 0;
}
int retVal = libp2p_message_protobuf_encode(in, *buffer, *buffer_size, buffer_size);
if (retVal == 0) {
free(*buffer);
*buffer = NULL;
*buffer_size = 0;
}
return retVal;
}
int libp2p_message_protobuf_encode(const struct Libp2pMessage* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written) {
// data & data_size
size_t bytes_used = 0;
*bytes_written = 0;

View File

@ -50,6 +50,29 @@ void libp2p_record_free(struct Libp2pRecord* in) {
}
}
/**
* Protobuf a record, allocating memory
* @param in the record to protobuf
* @param buffer where to put the results
* @param buffer_size the size of the results
* @returns true(1) on success, false(0) otherwise
*/
int libp2p_record_protobuf_allocate_and_encode(const struct Libp2pRecord* in, unsigned char** buffer, size_t* buffer_size) {
*buffer_size = libp2p_record_protobuf_encode_size(in);
*buffer = malloc(*buffer_size);
if (*buffer == NULL) {
*buffer_size = 0;
return 0;
}
int retVal = libp2p_record_protobuf_encode(in, *buffer, *buffer_size, buffer_size);
if (retVal == 0) {
free(*buffer);
*buffer = NULL;
*buffer_size = 0;
}
return retVal;
}
/**
* Convert a Libp2pRecord into protobuf format
* @param in the Libp2pRecord to convert

View File

@ -285,7 +285,8 @@ int libp2p_routing_dht_handle_find_node(struct SessionContext* session, struct L
// look through peer store
struct Libp2pPeer* peer = libp2p_peerstore_get_peer(peerstore, message->key, message->key_size);
if (peer != NULL) {
message->closer_peer_head = peer->addr_head;
message->provider_peer_head = libp2p_utils_linked_list_new();
message->provider_peer_head->item = peer;
if (!libp2p_routing_dht_protobuf_message(message, result_buffer, result_buffer_size)) {
return 0;
}