Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkClientSecure - copyability improvements and _timeout shadowing fixed #9632

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions libraries/NetworkClientSecure/src/NetworkClientSecure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void NetworkClientSecure::stop() {
stop_ssl_socket(sslclient.get());

_connected = false;
_peek = -1;
sslclient->peek_buf = -1;
_lastReadTimeout = 0;
_lastWriteTimeout = 0;
}
Expand Down Expand Up @@ -134,7 +134,7 @@ int NetworkClientSecure::connect(IPAddress ip, uint16_t port, const char *host,
log_i("Actual TLS start postponed.");
}

_lastError = ret;
sslclient->last_error = ret;

if (ret < 0) {
log_e("start_ssl_client: connect failed: %d", ret);
Expand Down Expand Up @@ -175,7 +175,7 @@ int NetworkClientSecure::connect(const char *host, uint16_t port, const char *ps
}

int ret = start_ssl_client(sslclient.get(), address, port, host, _timeout, NULL, false, NULL, NULL, pskIdent, psKey, _use_insecure, _alpn_protos);
_lastError = ret;
sslclient->last_error = ret;
if (ret < 0) {
log_e("start_ssl_client: connect failed %d", ret);
stop();
Expand All @@ -186,11 +186,11 @@ int NetworkClientSecure::connect(const char *host, uint16_t port, const char *ps
}

int NetworkClientSecure::peek() {
if (_peek >= 0) {
return _peek;
if (sslclient->peek_buf >= 0) {
return sslclient->peek_buf;
}
_peek = timedRead();
return _peek;
sslclient->peek_buf = timedRead();
return sslclient->peek_buf;
}

size_t NetworkClientSecure::write(uint8_t data) {
Expand Down Expand Up @@ -253,9 +253,9 @@ int NetworkClientSecure::read(uint8_t *buf, size_t size) {
if (!size) {
return 0;
}
if (_peek >= 0) {
buf[0] = _peek;
_peek = -1;
if (sslclient->peek_buf >= 0) {
buf[0] = sslclient->peek_buf;
sslclient->peek_buf = -1;
size--;
avail--;
if (!size || !avail) {
Expand All @@ -279,7 +279,7 @@ int NetworkClientSecure::available() {
return peek_net_receive(sslclient.get(), 0);
}

int peeked = (_peek >= 0), res = -1;
int peeked = (sslclient->peek_buf >= 0), res = -1;
if (!_connected) {
return peeked;
}
Expand Down Expand Up @@ -399,11 +399,9 @@ bool NetworkClientSecure::loadPrivateKey(Stream &stream, size_t size) {
}

int NetworkClientSecure::lastError(char *buf, const size_t size) {
if (!_lastError) {
return 0;
}
mbedtls_strerror(_lastError, buf, size);
return _lastError;
int lastError = sslclient->last_error;
mbedtls_strerror(lastError, buf, size);
return lastError;
}

void NetworkClientSecure::setHandshakeTimeout(unsigned long handshake_timeout) {
Expand Down
3 changes: 0 additions & 3 deletions libraries/NetworkClientSecure/src/NetworkClientSecure.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class NetworkClientSecure : public NetworkClient {
protected:
std::shared_ptr<sslclient_context> sslclient;

int _lastError = 0;
int _peek = -1;
int _timeout;
bool _use_insecure;
bool _stillinPlainStart = false;
const char *_CA_cert;
Expand Down
4 changes: 4 additions & 0 deletions libraries/NetworkClientSecure/src/ssl_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void ssl_init(sslclient_context *ssl_client) {
mbedtls_ssl_init(&ssl_client->ssl_ctx);
mbedtls_ssl_config_init(&ssl_client->ssl_conf);
mbedtls_ctr_drbg_init(&ssl_client->drbg_ctx);
ssl_client->peek_buf = -1;
}

int start_ssl_client(
Expand Down Expand Up @@ -368,12 +369,15 @@ void stop_ssl_socket(sslclient_context *ssl_client) {
// save only interesting fields
int handshake_timeout = ssl_client->handshake_timeout;
int socket_timeout = ssl_client->socket_timeout;
int last_err = ssl_client->last_error;

// reset embedded pointers to zero
memset(ssl_client, 0, sizeof(sslclient_context));

ssl_client->handshake_timeout = handshake_timeout;
ssl_client->socket_timeout = socket_timeout;
ssl_client->last_error = last_err;
ssl_client->peek_buf = -1;
}

int data_to_read(sslclient_context *ssl_client) {
Expand Down
4 changes: 4 additions & 0 deletions libraries/NetworkClientSecure/src/ssl_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ typedef struct sslclient_context {

unsigned long socket_timeout;
unsigned long handshake_timeout;

int last_error;
int peek_buf;

} sslclient_context;

void ssl_init(sslclient_context *ssl_client);
Expand Down