Skip to content

Commit 2df885a

Browse files
Enable multiple SSL connections by using AXTLS fd
axTLS lets us pass in a "file descriptor" which is used only by our own code and could be anything that fits into an int. We were passing in 0 all the time which means axtls would get confused if there were multiple simultaneous connections. Instead of 0, pass in (this), a pointer to our object, where we now have a normal class variable io_ctx, in place of the single static s_io_ctx. By doing so we guarantee multiple connections get unique multiple FDs, and remove the need for any static tracking of the io_ctx array/map.
1 parent f49df47 commit 2df885a

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ class SSLContext
7272
if (_ssl_ctx_refcnt == 0) {
7373
ssl_ctx_free(_ssl_ctx);
7474
}
75-
76-
s_io_ctx = nullptr;
7775
}
7876

7977
void ref()
@@ -96,14 +94,14 @@ class SSLContext
9694
if (_ssl) {
9795
/* Creating a new TLS session on top of a new TCP connection.
9896
ssl_free will want to send a close notify alert, but the old TCP connection
99-
is already gone at this point, so reset s_io_ctx. */
100-
s_io_ctx = nullptr;
97+
is already gone at this point, so reset io_ctx. */
98+
io_ctx = nullptr;
10199
ssl_free(_ssl);
102100
_available = 0;
103101
_read_ptr = nullptr;
104102
}
105-
s_io_ctx = ctx;
106-
_ssl = ssl_client_new(_ssl_ctx, 0, nullptr, 0, ext);
103+
io_ctx = ctx;
104+
_ssl = ssl_client_new(_ssl_ctx, reinterpret_cast<int>(this), nullptr, 0, ext);
107105
uint32_t t = millis();
108106

109107
while (millis() - t < timeout_ms && ssl_handshake_status(_ssl) != SSL_OK) {
@@ -116,8 +114,8 @@ class SSLContext
116114
}
117115

118116
void connectServer(ClientContext *ctx) {
119-
s_io_ctx = ctx;
120-
_ssl = ssl_server_new(_ssl_ctx, 0);
117+
io_ctx = ctx;
118+
_ssl = ssl_server_new(_ssl_ctx, reinterpret_cast<int>(this));
121119
_isServer = true;
122120

123121
int timeout_ms = 5000;
@@ -134,7 +132,7 @@ class SSLContext
134132

135133
void stop()
136134
{
137-
s_io_ctx = nullptr;
135+
io_ctx = nullptr;
138136
}
139137

140138
bool connected()
@@ -244,8 +242,7 @@ class SSLContext
244242

245243
static ClientContext* getIOContext(int fd)
246244
{
247-
(void) fd;
248-
return s_io_ctx;
245+
return reinterpret_cast<SSLContext*>(fd)->io_ctx;
249246
}
250247

251248
int loadServerX509Cert(const uint8_t *cert, int len) {
@@ -287,12 +284,11 @@ class SSLContext
287284
int _refcnt = 0;
288285
const uint8_t* _read_ptr = nullptr;
289286
size_t _available = 0;
290-
static ClientContext* s_io_ctx;
287+
ClientContext* io_ctx;
291288
};
292289

293290
SSL_CTX* SSLContext::_ssl_ctx = nullptr;
294291
int SSLContext::_ssl_ctx_refcnt = 0;
295-
ClientContext* SSLContext::s_io_ctx = nullptr;
296292

297293
WiFiClientSecure::WiFiClientSecure()
298294
{

0 commit comments

Comments
 (0)