From 8e48c9d542965a006e3bf66413177dd72205da72 Mon Sep 17 00:00:00 2001 From: xavierchanth Date: Thu, 28 Nov 2024 11:21:31 -0500 Subject: [PATCH 1/3] fix: make sure monitor read handles ret 0 from read as a failure --- packages/atclient/src/monitor.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index 77ab4bfb..e8508fcd 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -128,14 +128,14 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m } size_t off = chunksize * chunks; - int i = 0; + size_t i = 0; while (i < chunksize) { ret = mbedtls_ssl_read(&(monitor_conn->atserver_connection.ssl), (unsigned char *)buffer + off + i, 1); // successfully read if (buffer[off + i] == '\n') { buffer[off + i] = '\0'; done_reading = true; - break; + goto exit_loop; } // successfully read something, continue if (ret > 0) { @@ -158,11 +158,15 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m case 0: // transport closed without close notify default: // Other errors done_reading = true; - break; + if (ret == 0) { + ret = -1; + } + goto exit_loop; } } chunks = chunks + 1; } +exit_loop: if (ret <= 0) { // you should reconnect... message->type = ATCLIENT_MONITOR_ERROR_READ; message->error_read.error_code = ret; @@ -339,13 +343,11 @@ static int decrypt_notification(atclient *atclient, atclient_atnotification *not // holds shared encryption key in raw bytes (after base64 decode operation) const size_t sharedenckeysize = ATCHOPS_AES_256 / 8; unsigned char sharedenckey[sharedenckeysize]; - size_t sharedenckeylen = 0; // temporarily holds the shared encryption key in base64 const size_t sharedenckeybase64size = atchops_base64_encoded_size(sharedenckeysize); unsigned char sharedenckeybase64[sharedenckeybase64size]; memset(sharedenckeybase64, 0, sizeof(unsigned char) * sharedenckeybase64size); - size_t sharedenckeybase64len = 0; unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; From 9b9cb315a8af08a19e9679c9dad98572536f2ee4 Mon Sep 17 00:00:00 2001 From: xavierchanth Date: Thu, 28 Nov 2024 11:35:34 -0500 Subject: [PATCH 2/3] feat: add empty message type to monitor for timeouts --- packages/atclient/include/atclient/monitor.h | 2 ++ packages/atclient/src/monitor.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/atclient/include/atclient/monitor.h b/packages/atclient/include/atclient/monitor.h index 8ca8f4ed..57f161f2 100644 --- a/packages/atclient/include/atclient/monitor.h +++ b/packages/atclient/include/atclient/monitor.h @@ -19,6 +19,7 @@ enum atclient_monitor_response_type { ATCLIENT_MONITOR_MESSAGE_TYPE_NOTIFICATION, // indicates caller to access `notification` from the union ATCLIENT_MONITOR_MESSAGE_TYPE_DATA_RESPONSE, // indicates caller to access `data_response` from the union ATCLIENT_MONITOR_MESSAGE_TYPE_ERROR_RESPONSE, // indicates caller to access `error_response` from the union + ATCLIENT_MONITOR_MESSAGE_TYPE_EMPTY, // indicates that no message was received // the following 3 enums help indicate what type of error occurred when reading from the monitor connection, you will // expect one of these enums along with a non-zero return value from atclient_monitor_read @@ -26,6 +27,7 @@ enum atclient_monitor_response_type { // `error_read` from the union ATCLIENT_MONITOR_ERROR_PARSE_NOTIFICATION, ATCLIENT_MONITOR_ERROR_DECRYPT_NOTIFICATION, + }; // Represents error information when `ATCLIENT_MONITOR_ERROR_READ` is the message type given by atclient_monitor_read diff --git a/packages/atclient/src/monitor.c b/packages/atclient/src/monitor.c index e8508fcd..55402cf3 100644 --- a/packages/atclient/src/monitor.c +++ b/packages/atclient/src/monitor.c @@ -152,7 +152,10 @@ int atclient_monitor_read(atclient *monitor_conn, atclient *atclient, atclient_m case MBEDTLS_ERR_SSL_WANT_WRITE: // handshake incomplete usleep(10000); // Try again in 10 milliseconds break; - + // Timeout means nothing to read, return EMPTY message type + case MBEDTLS_ERR_SSL_TIMEOUT: + message->type = ATCLIENT_MONITOR_MESSAGE_TYPE_EMPTY; + return 0; // Monitor connection bad, must be discarded case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: // transport closed with close notify case 0: // transport closed without close notify From 1b801e3d7243e100839a5609b75394afe0dddfd1 Mon Sep 17 00:00:00 2001 From: xavierchanth Date: Thu, 28 Nov 2024 13:07:39 -0500 Subject: [PATCH 3/3] chore: update change log and version 0.3.3 --- packages/atclient/CHANGELOG.md | 5 +++++ packages/atclient/include/atclient/version.h | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/atclient/CHANGELOG.md b/packages/atclient/CHANGELOG.md index 2e1a3b2a..d8976681 100644 --- a/packages/atclient/CHANGELOG.md +++ b/packages/atclient/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.3.3 + +- fix: monitor resiliency + - Added a new monitor message which represents an empty message after a timeout + ## 0.3.2 - Fix unused include warnings in notify diff --git a/packages/atclient/include/atclient/version.h b/packages/atclient/include/atclient/version.h index bdf90ff8..4fdfbfd3 100644 --- a/packages/atclient/include/atclient/version.h +++ b/packages/atclient/include/atclient/version.h @@ -4,7 +4,7 @@ extern "C" { #endif -#define ATCLIENT_ATSDK_VERSION "0.3.2" +#define ATCLIENT_ATSDK_VERSION "0.3.3" #ifdef __cplusplus }