From e886fe328872815faf4400717718917fdc4ac076 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 13 Apr 2017 09:30:28 -0500 Subject: [PATCH] Handling finding of peer via swarm --- include/libp2p/record/message.h | 14 ++++++++++++-- include/libp2p/record/record.h | 10 ++++++++++ net/multistream.c | 8 ++++---- peer/peerstore.c | 9 +++++---- record/message.c | 28 ++++++++++++++++++++++++++-- record/record.c | 23 +++++++++++++++++++++++ routing/dht_protocol.c | 3 ++- 7 files changed, 82 insertions(+), 13 deletions(-) diff --git a/include/libp2p/record/message.h b/include/libp2p/record/message.h index 29cf99b..8d3f6aa 100644 --- a/include/libp2p/record/message.h +++ b/include/libp2p/record/message.h @@ -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 diff --git a/include/libp2p/record/record.h b/include/libp2p/record/record.h index eafc8bd..6957394 100644 --- a/include/libp2p/record/record.h +++ b/include/libp2p/record/record.h @@ -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 diff --git a/net/multistream.c b/net/multistream.c index 83a8079..8569dd8 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -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]; diff --git a/peer/peerstore.c b/peer/peerstore.c index e7e772c..a288e2d 100644 --- a/peer/peerstore.c +++ b/peer/peerstore.c @@ -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; } diff --git a/record/message.c b/record/message.c index e8223c0..46ac064 100644 --- a/record/message.c +++ b/record/message.c @@ -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; diff --git a/record/record.c b/record/record.c index 2675d53..0601482 100644 --- a/record/record.c +++ b/record/record.c @@ -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 diff --git a/routing/dht_protocol.c b/routing/dht_protocol.c index 3c38a9e..5bb4352 100644 --- a/routing/dht_protocol.c +++ b/routing/dht_protocol.c @@ -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; }