IXSocketMbedTLS: better error handling in close and connect

This commit is contained in:
Benjamin Sergeant 2019-06-06 14:59:22 -07:00
parent 12e664fc61
commit d2747487e3
2 changed files with 24 additions and 3 deletions

View File

@ -1,6 +1,10 @@
# Changelog # Changelog
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [Unreleased] - 2019-06-xx
### Changed
- IXSocketMbedTLS: better error handling in close and connect
## [3.1.2] - 2019-06-06 ## [3.1.2] - 2019-06-06
### Added ### Added
- ws connect has a -x option to disable per message deflate - ws connect has a -x option to disable per message deflate

View File

@ -24,6 +24,8 @@ namespace ix
bool SocketMbedTLS::init(const std::string& host, std::string& errMsg) bool SocketMbedTLS::init(const std::string& host, std::string& errMsg)
{ {
std::lock_guard<std::mutex> lock(_mutex);
mbedtls_ssl_init(&_ssl); mbedtls_ssl_init(&_ssl);
mbedtls_ssl_config_init(&_conf); mbedtls_ssl_config_init(&_conf);
mbedtls_ctr_drbg_init(&_ctr_drbg); mbedtls_ctr_drbg_init(&_ctr_drbg);
@ -75,15 +77,24 @@ namespace ix
std::string& errMsg, std::string& errMsg,
const CancellationRequest& isCancellationRequested) const CancellationRequest& isCancellationRequested)
{ {
_sockfd = SocketConnect::connect(host, port, errMsg, isCancellationRequested); {
if (_sockfd == -1) return false; std::lock_guard<std::mutex> lock(_mutex);
if (!init(host, errMsg)) return false; _sockfd = SocketConnect::connect(host, port, errMsg, isCancellationRequested);
if (_sockfd == -1) return false;
}
if (!init(host, errMsg))
{
close();
return false;
}
mbedtls_ssl_set_bio(&_ssl, &_sockfd, mbedtls_net_send, mbedtls_net_recv, NULL); mbedtls_ssl_set_bio(&_ssl, &_sockfd, mbedtls_net_send, mbedtls_net_recv, NULL);
int res; int res;
do do
{ {
std::lock_guard<std::mutex> lock(_mutex);
res = mbedtls_ssl_handshake(&_ssl); res = mbedtls_ssl_handshake(&_ssl);
} }
while (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE); while (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE);
@ -95,6 +106,8 @@ namespace ix
errMsg = "error in handshake : "; errMsg = "error in handshake : ";
errMsg += buf; errMsg += buf;
close();
return false; return false;
} }
@ -103,10 +116,14 @@ namespace ix
void SocketMbedTLS::close() void SocketMbedTLS::close()
{ {
std::lock_guard<std::mutex> lock(_mutex);
mbedtls_ssl_free(&_ssl); mbedtls_ssl_free(&_ssl);
mbedtls_ssl_config_free(&_conf); mbedtls_ssl_config_free(&_conf);
mbedtls_ctr_drbg_free(&_ctr_drbg); mbedtls_ctr_drbg_free(&_ctr_drbg);
mbedtls_entropy_free(&_entropy); mbedtls_entropy_free(&_entropy);
Socket::close();
} }
ssize_t SocketMbedTLS::send(char* buf, size_t nbyte) ssize_t SocketMbedTLS::send(char* buf, size_t nbyte)