From e922c7ef1462e849c32f767d9450bb4dcacde302 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 14 Sep 2017 11:35:16 -0500 Subject: [PATCH] Implemented put_value --- routing/dht_protocol.c | 38 +++++++++++++++++++++++++++++++------- secio/secio.c | 16 ++++++++-------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/routing/dht_protocol.c b/routing/dht_protocol.c index dd34903..e56fba0 100644 --- a/routing/dht_protocol.c +++ b/routing/dht_protocol.c @@ -320,7 +320,7 @@ int libp2p_routing_dht_handle_add_provider(struct SessionContext* session, struc * @param message the message * @param peerstore the peerstore * @param providerstore the providerstore - * @param result_buffer the results + * @param result_buffer the results, as a protobuf'd KademliaMessage * @param result_buffer_size the size of the results * @returns true(1) on success, otherwise false(0) */ @@ -364,14 +364,38 @@ int libp2p_routing_dht_handle_get_value(struct SessionContext* session, struct K * @param message the message * @param peerstore the peerstore * @param providerstore the providerstore - * @param result_buffer the results - * @param result_buffer_size the size of the results * @returns true(1) on success, otherwise false(0) */ int libp2p_routing_dht_handle_put_value(struct SessionContext* session, struct KademliaMessage* message, - struct Peerstore* peerstore, struct ProviderStore* providerstore, unsigned char** result_buffer, size_t *result_buffer_size) { - //TODO: implement this - return 0; + struct Peerstore* peerstore, struct ProviderStore* providerstore) { + + if (message->record == NULL) + return 0; + + struct DatastoreRecord* record = libp2p_datastore_record_new(); + if (record == NULL) + return 0; + + // set the key from the message->record->key + record->key_size = message->record->key_size; + record->key = (uint8_t*) malloc(record->key_size); + if (record->key == NULL) { + libp2p_datastore_record_free(record); + return 0; + } + memcpy(record->key, message->record->key, record->key_size); + // set the value from the message->record->value + record->value_size = message->record->value_size; + record->value = (uint8_t*) malloc(record->value_size); + if (record->value == NULL) { + libp2p_datastore_record_free(record); + return 0; + } + memcpy(record->value, message->record->value, record->value_size); + + int retVal = session->datastore->datastore_put(record, session->datastore); + libp2p_datastore_record_free(record); + return retVal; } /** @@ -423,7 +447,7 @@ int libp2p_routing_dht_handle_message(struct SessionContext* session, struct Pee // handle message switch(message->message_type) { case(MESSAGE_TYPE_PUT_VALUE): // store a value in local storage - libp2p_routing_dht_handle_put_value(session, message, peerstore, providerstore, &result_buffer, &result_buffer_size); + libp2p_routing_dht_handle_put_value(session, message, peerstore, providerstore); break; case(MESSAGE_TYPE_GET_VALUE): // get a value from local storage libp2p_routing_dht_handle_get_value(session, message, peerstore, providerstore, &result_buffer, &result_buffer_size); diff --git a/secio/secio.c b/secio/secio.c index 1570056..46e1eda 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -933,7 +933,7 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva if (bytes_written != propose_out_size) { libp2p_logger_error("secio", "Sent propose_out, but did not write the correct number of bytes. Should be %d but was %d.\n", propose_out_size, bytes_written); } else { - libp2p_logger_debug("secio", "Sent propose out.\n"); + //libp2p_logger_debug("secio", "Sent propose out.\n"); } // try to get the Propse struct from the remote peer @@ -942,7 +942,7 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva libp2p_logger_error("secio", "Unable to get the remote's Propose struct.\n"); goto exit; } else { - libp2p_logger_debug("secio", "Received their propose struct.\n"); + //libp2p_logger_debug("secio", "Received their propose struct.\n"); } if (!libp2p_secio_propose_protobuf_decode(propose_in_bytes, propose_in_size -1, &propose_in)) { @@ -1040,13 +1040,13 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva libp2p_secio_exchange_protobuf_encode(exchange_out, exchange_out_protobuf, exchange_out_protobuf_size, &bytes_written); exchange_out_protobuf_size = bytes_written; - libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Writing exchange_out\n"); + //libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Writing exchange_out\n"); bytes_written = libp2p_secio_unencrypted_write(local_session, exchange_out_protobuf, exchange_out_protobuf_size); if (exchange_out_protobuf_size != bytes_written) { libp2p_logger_error("secio", "Unable to write exchange_out\n"); goto exit; } else { - libp2p_logger_debug("secio", "Sent exchange_out.\n"); + //libp2p_logger_debug("secio", "Sent exchange_out.\n"); } free(exchange_out_protobuf); exchange_out_protobuf = NULL; @@ -1060,7 +1060,7 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva libp2p_peer_handle_connection_error(remote_peer); goto exit; } else { - libp2p_logger_debug("secio", "Read exchange packet.\n"); + //libp2p_logger_debug("secio", "Read exchange packet.\n"); } libp2p_secio_exchange_protobuf_decode(results, results_size, &exchange_in); free(results); @@ -1137,7 +1137,7 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva libp2p_secio_initialize_crypto(local_session); // send their nonce to verify encryption works - libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Sending their nonce\n"); + //libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Sending their nonce\n"); if (libp2p_secio_encrypted_write(local_session, (unsigned char*)local_session->remote_nonce, 16) <= 0) { libp2p_logger_error("secio", "Encrytped write returned 0 or less.\n"); goto exit; @@ -1174,7 +1174,7 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva retVal = 1; - libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake complete\n"); + //libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake complete\n"); exit: if (propose_in_bytes != NULL) free(propose_in_bytes); @@ -1197,7 +1197,7 @@ int libp2p_secio_handshake(struct SessionContext* local_session, struct RsaPriva libp2p_secio_propose_free(propose_in); if (retVal == 1) { - libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake success!\n"); + //libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake success!\n"); } else { libp2p_logger_log("secio", LOGLEVEL_DEBUG, "Handshake returning false\n"); }