Skip to content

Commit 45e7976

Browse files
Fix stopAllExcept with WiFiClientSecure (#8136)
Fixes #8079 Because WiFiClientSecure inherits WiFiClient, and WiFiClientSecureCtx also inherits WiFiClient, they both end up in the list of TCP connections that are used for WiFiClient::stopAllExcept(). This would cause the underlying SSL connection to be closed whenever you attempted to stopAllExcept(WiFiClientSecure) Fix by adding a "_owned"(by) pointer in the WiFiClient object which points to nullptr (default case) or to the associated lower-layer connection. When stopping all connections except one, only look at the lowermost connections.
1 parent 2f37c96 commit 45e7976

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

libraries/ESP8266WiFi/src/WiFiClient.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ WiFiClient* SList<WiFiClient>::_s_first = 0;
7777

7878

7979
WiFiClient::WiFiClient()
80-
: _client(0)
80+
: _client(0), _owned(0)
8181
{
8282
_timeout = 5000;
8383
WiFiClient::_add(this);
8484
}
8585

8686
WiFiClient::WiFiClient(ClientContext* client)
87-
: _client(client)
87+
: _client(client), _owned(0)
8888
{
8989
_timeout = 5000;
9090
_client->ref();
@@ -106,6 +106,7 @@ WiFiClient::WiFiClient(const WiFiClient& other)
106106
_client = other._client;
107107
_timeout = other._timeout;
108108
_localPort = other._localPort;
109+
_owned = other._owned;
109110
if (_client)
110111
_client->ref();
111112
WiFiClient::_add(this);
@@ -118,6 +119,7 @@ WiFiClient& WiFiClient::operator=(const WiFiClient& other)
118119
_client = other._client;
119120
_timeout = other._timeout;
120121
_localPort = other._localPort;
122+
_owned = other._owned;
121123
if (_client)
122124
_client->ref();
123125
return *this;
@@ -382,9 +384,18 @@ void WiFiClient::stopAll()
382384

383385
void WiFiClient::stopAllExcept(WiFiClient* except)
384386
{
387+
// Stop all will look at the lowest-level wrapper connections only
388+
while (except->_owned) {
389+
except = except->_owned;
390+
}
385391
for (WiFiClient* it = _s_first; it; it = it->_next) {
386-
if (it != except) {
387-
it->stop();
392+
WiFiClient* conn = it;
393+
// Find the lowest-level owner of the current list entry
394+
while (conn->_owned) {
395+
conn = conn->_owned;
396+
}
397+
if (conn != except) {
398+
conn->stop();
388399
}
389400
}
390401
}

libraries/ESP8266WiFi/src/WiFiClient.h

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class WiFiClient : public Client, public SList<WiFiClient> {
144144
void _err(int8_t err);
145145

146146
ClientContext* _client;
147+
WiFiClient* _owned;
147148
static uint16_t _localPort;
148149
};
149150

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ class WiFiClientSecure : public WiFiClient {
232232

233233
public:
234234

235-
WiFiClientSecure():_ctx(new WiFiClientSecureCtx()) { }
236-
WiFiClientSecure(const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx) { }
235+
WiFiClientSecure():_ctx(new WiFiClientSecureCtx()) { _owned = _ctx.get(); }
236+
WiFiClientSecure(const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx) { if (_ctx) _owned = _ctx.get(); }
237237
~WiFiClientSecure() override { _ctx = nullptr; }
238238

239239
WiFiClientSecure& operator=(const WiFiClientSecure&) = default; // The shared-ptrs handle themselves automatically

0 commit comments

Comments
 (0)