Uniform behaviour of WiFiClientSecure and WiFiClient setTimeout() (#6562)

* Uniform timeout WiFiClient-WiFiClientSecure

* Added missing prototype

* Add socket check on setTimeout
This commit is contained in:
Gonzalo Brusco
2022-04-26 08:46:07 -03:00
committed by GitHub
parent a9d33de70e
commit 823fe77e41
4 changed files with 64 additions and 27 deletions

View File

@ -32,6 +32,7 @@
WiFiClientSecure::WiFiClientSecure()
{
_connected = false;
_timeout = 30000; // Same default as ssl_client
sslclient = new sslclient_context;
ssl_init(sslclient);
@ -52,7 +53,7 @@ WiFiClientSecure::WiFiClientSecure()
WiFiClientSecure::WiFiClientSecure(int sock)
{
_connected = false;
_timeout = 0;
_timeout = 30000; // Same default as ssl_client
sslclient = new sslclient_context;
ssl_init(sslclient);
@ -128,9 +129,6 @@ int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *CA_cert,
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *CA_cert, const char *cert, const char *private_key)
{
if(_timeout > 0){
sslclient->handshake_timeout = _timeout;
}
int ret = start_ssl_client(sslclient, host, port, _timeout, CA_cert, _use_ca_bundle, cert, private_key, NULL, NULL, _use_insecure, _alpn_protos);
_lastError = ret;
if (ret < 0) {
@ -148,9 +146,6 @@ int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *pskIdent,
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey) {
log_v("start_ssl_client with PSK");
if(_timeout > 0){
sslclient->handshake_timeout = _timeout;
}
int ret = start_ssl_client(sslclient, host, port, _timeout, NULL, false, NULL, NULL, pskIdent, psKey, _use_insecure, _alpn_protos);
_lastError = ret;
if (ret < 0) {
@ -365,3 +360,27 @@ void WiFiClientSecure::setAlpnProtocols(const char **alpn_protos)
{
_alpn_protos = alpn_protos;
}
int WiFiClientSecure::setTimeout(uint32_t seconds)
{
_timeout = seconds * 1000;
if (sslclient->socket >= 0) {
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
return -1;
}
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
}
else {
return 0;
}
}
int WiFiClientSecure::setSocketOption(int option, char* value, size_t len)
{
int res = setsockopt(sslclient->socket, SOL_SOCKET, option, value, len);
if(res < 0) {
log_e("%X : %d", option, errno);
}
return res;
}

View File

@ -32,7 +32,7 @@ protected:
int _lastError = 0;
int _peek = -1;
int _timeout = 0;
int _timeout;
bool _use_insecure;
const char *_CA_cert;
const char *_cert;
@ -79,7 +79,8 @@ public:
void setAlpnProtocols(const char **alpn_protos);
const mbedtls_x509_crt* getPeerCertificate() { return mbedtls_ssl_get_peer_cert(&sslclient->ssl_ctx); };
bool getFingerprintSHA256(uint8_t sha256_result[32]) { return get_peer_fingerprint(sslclient, sha256_result); };
int setTimeout(uint32_t seconds){ return 0; }
int setTimeout(uint32_t seconds);
int setSocketOption(int option, char* value, size_t len);
operator bool()
{