Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
fix(endpoint): Use the logger
Browse files Browse the repository at this point in the history
Add the logger for the endpoint and the related source code.
  • Loading branch information
marktwtn committed Jun 3, 2020
1 parent e530f90 commit 79b6b43
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 19 deletions.
45 changes: 32 additions & 13 deletions endpoint/endpoint_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "common/logger.h"
#include "http_parser.h"
#include "utils/cipher.h"
#include "utils/connectivity/conn_http.h"
Expand All @@ -31,9 +32,31 @@ static const char* SSL_SEED = STR(ENDPOINT_SSL_SEED);

#define SEND_TRANSACTION_API "transaction/"

status_t send_transaction_information(int value, const char* message, const char* message_fmt, const char* tag,
const char* address, const char* next_address, const uint8_t* private_key,
const char* device_id, uint8_t* iv) {
#define ENDPOINT_LOGGER "endpoint"

static logger_id_t logger_id;

void endpoint_init() {
ta_logger_init();
// Logger initialization of endpoint
logger_id = logger_helper_enable(ENDPOINT_LOGGER, LOGGER_DEBUG, true);

// Logger initialization of other included components
cipher_logger_init();
}

void endpoint_destroy() {
// Logger release of other included components
cipher_logger_release();

// Logger release of endpoint
logger_helper_release(logger_id);
logger_helper_destroy();
}

status_t send_transaction_information(int value, const char* message, const char* message_fmt,
const char* tag, const char* address, const char* next_address,
const uint8_t* private_key, const char* device_id, uint8_t* iv) {
char tryte_msg[MAX_MSG_LEN] = {0};
char msg[MAX_MSG_LEN] = {0};
char req_body[MAX_MSG_LEN] = {0};
Expand All @@ -42,8 +65,7 @@ status_t send_transaction_information(int value, const char* message, const char

int ret = snprintf((char*)raw_msg, MAX_MSG_LEN, "%s:%s", next_address, message);
if (ret < 0) {
// FIXME: Replace to default logger
fprintf(stderr, "message is to long");
ta_log_error("The message is too long.\n");
return SC_ENDPOINT_SEND_TRANSFER;
}

Expand All @@ -65,9 +87,8 @@ status_t send_transaction_information(int value, const char* message, const char
ret = aes_encrypt(&encrypt_ctx);
memcpy(iv, encrypt_ctx.iv, AES_IV_SIZE);

// FIXME: Replace to default logger
if (ret != SC_OK) {
fprintf(stderr, "%s\n", "encrypt msg error");
if (ret != RET_OK) {
ta_log_error("Encrypt message error.\n");
return ret;
}
serialize_msg(&encrypt_ctx, msg, &msg_len);
Expand All @@ -77,14 +98,12 @@ status_t send_transaction_information(int value, const char* message, const char

ret = snprintf(req_body, MAX_MSG_LEN, REQ_BODY, value, tryte_msg, message_fmt, tag, address);
if (ret < 0) {
// FIXME: Replace to default logger
fprintf(stderr, "message is to long");
ta_log_error("The message is too long.\n");
return SC_ENDPOINT_SEND_TRANSFER;
}

if (send_https_msg(HOST, PORT, SEND_TRANSACTION_API, req_body, SSL_SEED) != SC_OK) {
// FIXME: Replace to default logger
fprintf(stderr, "%s\n", "send http message error");
if (send_https_msg(HOST, PORT, SEND_TRANSACTION_API, req_body, MAX_MSG_LEN, SSL_SEED) != RET_OK) {
ta_log_error("http message sending error.\n");
return SC_ENDPOINT_SEND_TRANSFER;
}

Expand Down
10 changes: 10 additions & 0 deletions endpoint/endpoint_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
#include <stdint.h>
#include "common/ta_errors.h"

/**
* @brief Initialization of endpoint
*/
void endpoint_init(void);

/**
* @brief Destruction of endpoint
*/
void endpoint_destroy(void);

/**
* @brief Send transaction information to tangle accelerator
*
Expand Down
4 changes: 4 additions & 0 deletions endpoint/test_endpoint_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ void test_endpoint(void) {

int main(void) {
UNITY_BEGIN();
endpoint_init();

RUN_TEST(test_endpoint);

endpoint_destroy();
return UNITY_END();
}
7 changes: 7 additions & 0 deletions tests/unit-test/test_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,15 @@ void test_cipher2(void) {
int main(void) {
UNITY_BEGIN();

// Initialize logger
if (ta_logger_init() != SC_OK) {
return EXIT_FAILURE;
}

cipher_logger_init();
RUN_TEST(test_cipher1);
RUN_TEST(test_cipher2);
cipher_logger_release();

return UNITY_END();
}
1 change: 1 addition & 0 deletions utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ cc_library(
hdrs = ["cipher.h"],
deps = [
"//common:ta_errors",
"//common:ta_logger",
"@mbedtls_2_16_6",
],
)
Expand Down
26 changes: 20 additions & 6 deletions utils/cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "common/logger.h"
#include "mbedtls/aes.h"
#include "mbedtls/md.h"
#include "mbedtls/platform_util.h"

#define MAX_TIMESTAMP_LEN 20
#define CIPHER_LOGGER "cipher"

static logger_id_t logger_id;

void cipher_logger_init() { logger_id = logger_helper_enable(CIPHER_LOGGER, LOGGER_DEBUG, true); }

int cipher_logger_release() {
logger_helper_release(logger_id);
if (logger_helper_destroy() != RC_OK) {
ta_log_error("Destroying logger failed %s.\n", CIPHER_LOGGER);
return EXIT_FAILURE;
}

return 0;
}

status_t aes_decrypt(ta_cipher_ctx* cipher_ctx) {
// FIXME: Add logger and some checks here
// FIXME: Add some checks here
mbedtls_aes_context ctx;
mbedtls_md_context_t sha_ctx;
int status;
Expand Down Expand Up @@ -84,15 +100,14 @@ status_t aes_decrypt(ta_cipher_ctx* cipher_ctx) {
}
status = SC_OK;
exit:
// FIXME: Use default logger instead
if (!err) fprintf(stderr, "%s\n", err);
if (!err) ta_log_error("%s\n", err);
mbedtls_aes_free(&ctx);
mbedtls_md_free(&sha_ctx);
return status;
}

status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
// FIXME: Add logger and some checks here
// FIXME: Add some checks here
char* err = NULL;
int status = 0;
uint8_t* plaintext = cipher_ctx->plaintext;
Expand Down Expand Up @@ -172,8 +187,7 @@ status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
memcpy(cipher_ctx->hmac, digest, TA_AES_HMAC_SIZE);
status = SC_OK;
exit:
// FIXME: Use default logger instead
if (!err) fprintf(stderr, "%s\n", err);
if (!err) ta_log_error("%s\n", err);
mbedtls_aes_free(&ctx);
mbedtls_md_free(&sha_ctx);
return status;
Expand Down
14 changes: 14 additions & 0 deletions utils/cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ typedef struct ta_cipher_ctx {
uint64_t timestamp; /**< Timestamp for generate hmac */
} ta_cipher_ctx;

/**
* @brief Initialize logger of cipher
*/
void cipher_logger_init();

/**
* @brief Release logger of cipher
*
* @return
* - zero on success
* - EXIT_FAILURE on error
*/
int cipher_logger_release();

/**
* @brief Encrypt plaintext
*
Expand Down

0 comments on commit 79b6b43

Please sign in to comment.