mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
WiFiClientSecure: implement connection timeout, fix connected method behaviour
This commit is contained in:
@ -66,7 +66,7 @@ class SSLContext {
|
|||||||
public:
|
public:
|
||||||
SSLContext() {
|
SSLContext() {
|
||||||
if (_ssl_ctx_refcnt == 0) {
|
if (_ssl_ctx_refcnt == 0) {
|
||||||
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0);
|
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS | SSL_CONNECT_IN_PARTS, 0);
|
||||||
}
|
}
|
||||||
++_ssl_ctx_refcnt;
|
++_ssl_ctx_refcnt;
|
||||||
}
|
}
|
||||||
@ -93,8 +93,21 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect(ClientContext* ctx) {
|
void connect(ClientContext* ctx, uint32_t timeout_ms) {
|
||||||
_ssl = ssl_client_new(_ssl_ctx, reinterpret_cast<int>(ctx), nullptr, 0);
|
_ssl = ssl_client_new(_ssl_ctx, reinterpret_cast<int>(ctx), nullptr, 0);
|
||||||
|
uint32_t t = millis();
|
||||||
|
|
||||||
|
while (millis() - t < timeout_ms && ssl_handshake_status(_ssl) != SSL_OK) {
|
||||||
|
uint8_t* data;
|
||||||
|
int rc = ssl_read(_ssl, &data);
|
||||||
|
if (rc < SSL_OK) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool connected() {
|
||||||
|
return _ssl != nullptr && ssl_handshake_status(_ssl) == SSL_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int read(uint8_t* dst, size_t size) {
|
int read(uint8_t* dst, size_t size) {
|
||||||
@ -246,7 +259,7 @@ int WiFiClientSecure::_connectSSL() {
|
|||||||
|
|
||||||
_ssl = new SSLContext;
|
_ssl = new SSLContext;
|
||||||
_ssl->ref();
|
_ssl->ref();
|
||||||
_ssl->connect(_client);
|
_ssl->connect(_client, 5000);
|
||||||
|
|
||||||
auto status = ssl_handshake_status(*_ssl);
|
auto status = ssl_handshake_status(*_ssl);
|
||||||
if (status != SSL_OK) {
|
if (status != SSL_OK) {
|
||||||
@ -266,6 +279,11 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {
|
|||||||
if (rc >= 0)
|
if (rc >= 0)
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
|
if (rc != SSL_CLOSE_NOTIFY) {
|
||||||
|
_ssl->unref();
|
||||||
|
_ssl = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,17 +336,25 @@ int WiFiClientSecure::available() {
|
|||||||
return _ssl->available();
|
return _ssl->available();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
SSL TCP RX data connected
|
||||||
|
null x x N
|
||||||
|
!null x Y Y
|
||||||
|
Y Y x Y
|
||||||
|
x N N N
|
||||||
|
err x N N
|
||||||
|
*/
|
||||||
uint8_t WiFiClientSecure::connected() {
|
uint8_t WiFiClientSecure::connected() {
|
||||||
if (!_client)
|
if (_ssl) {
|
||||||
return 0;
|
if (_ssl->available()) {
|
||||||
|
return true;
|
||||||
if (_client->state() == ESTABLISHED)
|
}
|
||||||
return 1;
|
if (_client && _client->state() == ESTABLISHED && _ssl->connected()) {
|
||||||
|
return true;
|
||||||
if (!_ssl)
|
}
|
||||||
return 0;
|
}
|
||||||
|
return false;
|
||||||
return _ssl->available() > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiClientSecure::stop() {
|
void WiFiClientSecure::stop() {
|
||||||
|
Reference in New Issue
Block a user