Skip to content

Commit

Permalink
Merge pull request #452 from atsign-foundation/fix-monitor-ret0
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierChanth authored Nov 28, 2024
2 parents aa59194 + 1b801e3 commit a25c26f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions packages/atclient/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/atclient/include/atclient/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ 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
ATCLIENT_MONITOR_ERROR_READ, // could be a read timeout or some other error, indicates the caller to access
// `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
Expand Down
2 changes: 1 addition & 1 deletion packages/atclient/include/atclient/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern "C" {
#endif

#define ATCLIENT_ATSDK_VERSION "0.3.2"
#define ATCLIENT_ATSDK_VERSION "0.3.3"

#ifdef __cplusplus
}
Expand Down
17 changes: 11 additions & 6 deletions packages/atclient/src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -152,17 +152,24 @@ 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
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;
Expand Down Expand Up @@ -339,13 +346,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];

Expand Down

0 comments on commit a25c26f

Please sign in to comment.