Skip to content

Commit 73b008f

Browse files
committed
fix for multiple servers (use std::map)
1 parent 12f2733 commit 73b008f

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ extern "C"
4040
#include "include/ClientContext.h"
4141
#include "c_types.h"
4242

43+
#include <map> // is it too much?
44+
4345
#ifdef DEBUG_ESP_SSL
4446
#define DEBUG_SSL
4547
#endif
@@ -59,6 +61,7 @@ class SSLContext
5961
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS | SSL_CONNECT_IN_PARTS | SSL_READ_BLOCKING | SSL_NO_DEFAULT_KEY, 0);
6062
}
6163
++_ssl_ctx_refcnt;
64+
_fd = ++_fdcounter;
6265
}
6366

6467
~SSLContext()
@@ -72,8 +75,6 @@ class SSLContext
7275
if (_ssl_ctx_refcnt == 0) {
7376
ssl_ctx_free(_ssl_ctx);
7477
}
75-
76-
s_io_ctx = nullptr;
7778
}
7879

7980
void ref()
@@ -93,11 +94,11 @@ class SSLContext
9394
SSL_EXTENSIONS* ext = ssl_ext_new();
9495
ssl_ext_set_host_name(ext, hostName);
9596
ssl_ext_set_max_fragment_size(ext, 4096);
96-
s_io_ctx = ctx;
97+
s_io_ctx[_fd] = ctx;
9798
if (_ssl) {
9899
ssl_free(_ssl);
99100
}
100-
_ssl = ssl_client_new(_ssl_ctx, 0, nullptr, 0, ext);
101+
_ssl = ssl_client_new(_ssl_ctx, _fd, nullptr, 0, ext);
101102
uint32_t t = millis();
102103

103104
while (millis() - t < timeout_ms && ssl_handshake_status(_ssl) != SSL_OK) {
@@ -110,8 +111,8 @@ class SSLContext
110111
}
111112

112113
void connectServer(ClientContext *ctx) {
113-
s_io_ctx = ctx;
114-
_ssl = ssl_server_new(_ssl_ctx, 0);
114+
s_io_ctx[_fd] = ctx;
115+
_ssl = ssl_server_new(_ssl_ctx, _fd);
115116
_isServer = true;
116117

117118
int timeout_ms = 5000;
@@ -128,7 +129,6 @@ class SSLContext
128129

129130
void stop()
130131
{
131-
s_io_ctx = nullptr;
132132
}
133133

134134
bool connected()
@@ -238,8 +238,7 @@ class SSLContext
238238

239239
static ClientContext* getIOContext(int fd)
240240
{
241-
(void) fd;
242-
return s_io_ctx;
241+
return s_io_ctx[fd];
243242
}
244243

245244
int loadServerX509Cert(const uint8_t *cert, int len) {
@@ -277,16 +276,19 @@ class SSLContext
277276
bool _isServer = false;
278277
static SSL_CTX* _ssl_ctx;
279278
static int _ssl_ctx_refcnt;
279+
static int _fdcounter; // increased in constructor
280+
int _fd; // axtls file descriptor
280281
SSL* _ssl = nullptr;
281282
int _refcnt = 0;
282283
const uint8_t* _read_ptr = nullptr;
283284
size_t _available = 0;
284-
static ClientContext* s_io_ctx;
285+
static std::map<int, ClientContext*> s_io_ctx;
285286
};
286287

287288
SSL_CTX* SSLContext::_ssl_ctx = nullptr;
288289
int SSLContext::_ssl_ctx_refcnt = 0;
289-
ClientContext* SSLContext::s_io_ctx = nullptr;
290+
int SSLContext::_fdcounter = 0;
291+
std::map<int, ClientContext*> SSLContext::s_io_ctx;
290292

291293
WiFiClientSecure::WiFiClientSecure()
292294
{

0 commit comments

Comments
 (0)