Skip to content

Commit 2c40d82

Browse files
committed
WiFiClientSecure: implement connection timeout, fix connected method behaviour
1 parent efa35e2 commit 2c40d82

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

+39-13
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class SSLContext {
6666
public:
6767
SSLContext() {
6868
if (_ssl_ctx_refcnt == 0) {
69-
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0);
69+
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS | SSL_CONNECT_IN_PARTS, 0);
7070
}
7171
++_ssl_ctx_refcnt;
7272
}
@@ -93,8 +93,21 @@ class SSLContext {
9393
}
9494
}
9595

96-
void connect(ClientContext* ctx) {
96+
void connect(ClientContext* ctx, uint32_t timeout_ms) {
9797
_ssl = ssl_client_new(_ssl_ctx, reinterpret_cast<int>(ctx), nullptr, 0);
98+
uint32_t t = millis();
99+
100+
while (millis() - t < timeout_ms && ssl_handshake_status(_ssl) != SSL_OK) {
101+
uint8_t* data;
102+
int rc = ssl_read(_ssl, &data);
103+
if (rc < SSL_OK) {
104+
break;
105+
}
106+
}
107+
}
108+
109+
bool connected() {
110+
return _ssl != nullptr && ssl_handshake_status(_ssl) == SSL_OK;
98111
}
99112

100113
int read(uint8_t* dst, size_t size) {
@@ -246,7 +259,7 @@ int WiFiClientSecure::_connectSSL() {
246259

247260
_ssl = new SSLContext;
248261
_ssl->ref();
249-
_ssl->connect(_client);
262+
_ssl->connect(_client, 5000);
250263

251264
auto status = ssl_handshake_status(*_ssl);
252265
if (status != SSL_OK) {
@@ -266,6 +279,11 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {
266279
if (rc >= 0)
267280
return rc;
268281

282+
if (rc != SSL_CLOSE_NOTIFY) {
283+
_ssl->unref();
284+
_ssl = nullptr;
285+
}
286+
269287
return 0;
270288
}
271289

@@ -318,17 +336,25 @@ int WiFiClientSecure::available() {
318336
return _ssl->available();
319337
}
320338

321-
uint8_t WiFiClientSecure::connected() {
322-
if (!_client)
323-
return 0;
324-
325-
if (_client->state() == ESTABLISHED)
326-
return 1;
327339

328-
if (!_ssl)
329-
return 0;
330-
331-
return _ssl->available() > 0;
340+
/*
341+
SSL TCP RX data connected
342+
null x x N
343+
!null x Y Y
344+
Y Y x Y
345+
x N N N
346+
err x N N
347+
*/
348+
uint8_t WiFiClientSecure::connected() {
349+
if (_ssl) {
350+
if (_ssl->available()) {
351+
return true;
352+
}
353+
if (_client && _client->state() == ESTABLISHED && _ssl->connected()) {
354+
return true;
355+
}
356+
}
357+
return false;
332358
}
333359

334360
void WiFiClientSecure::stop() {

0 commit comments

Comments
 (0)