Skip to content

Commit 098c71c

Browse files
committed
Improve receive handling in TLS support (esp8266#43)
1 parent 2a297ca commit 098c71c

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
lines changed

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extern "C"
2929
}
3030
#include <errno.h>
3131
#include "debug.h"
32+
#include "cbuf.h"
3233
#include "ESP8266WiFi.h"
3334
#include "WiFiClientSecure.h"
3435
#include "WiFiClient.h"
@@ -41,15 +42,23 @@ extern "C"
4142
#include "include/ClientContext.h"
4243
#include "c_types.h"
4344

45+
//#define DEBUG_SSL
46+
47+
#ifdef DEBUG_SSL
48+
#define SSL_DEBUG_OPTS SSL_DISPLAY_STATES
49+
#else
50+
#define SSL_DEBUG_OPTS 0
51+
#endif
4452

4553
class SSLContext {
4654
public:
4755
SSLContext() {
4856
if (_ssl_ctx_refcnt == 0) {
49-
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DISPLAY_STATES, 0);
57+
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0);
5058
}
5159
++_ssl_ctx_refcnt;
5260

61+
_rxbuf = new cbuf(1536);
5362
}
5463

5564
~SSLContext() {
@@ -62,6 +71,8 @@ class SSLContext {
6271
if (_ssl_ctx_refcnt == 0) {
6372
ssl_ctx_free(_ssl_ctx);
6473
}
74+
75+
delete _rxbuf;
6576
}
6677

6778
void ref() {
@@ -78,27 +89,71 @@ class SSLContext {
7889
_ssl = ssl_client_new(_ssl_ctx, reinterpret_cast<int>(ctx), nullptr, 0);
7990
}
8091

92+
int read(uint8_t* dst, size_t size) {
93+
if (size > _rxbuf->getSize()) {
94+
_readAll();
95+
}
96+
return _rxbuf->read(reinterpret_cast<char*>(dst), size);
97+
}
98+
99+
int read() {
100+
optimistic_yield(100);
101+
if (!_rxbuf->getSize()) {
102+
_readAll();
103+
}
104+
return _rxbuf->read();
105+
}
106+
107+
int peek() {
108+
if (!_rxbuf->getSize()) {
109+
_readAll();
110+
}
111+
return _rxbuf->peek();
112+
}
113+
114+
int available() {
115+
optimistic_yield(100);
116+
return _rxbuf->getSize();
117+
}
118+
81119
operator SSL*() {
82120
return _ssl;
83121
}
84122

85123
protected:
124+
int _readAll() {
125+
uint8_t* data;
126+
int rc = ssl_read(_ssl, &data);
127+
if (rc <= 0)
128+
return 0;
129+
130+
if (rc > _rxbuf->room()) {
131+
DEBUGV("WiFiClientSecure rx overflow");
132+
rc = _rxbuf->room();
133+
}
134+
int result = 0;
135+
size_t sizeBefore = _rxbuf->getSize();
136+
if (rc)
137+
result = _rxbuf->write(reinterpret_cast<const char*>(data), rc);
138+
DEBUGV("*** rb: %d + %d = %d\r\n", sizeBefore, rc, _rxbuf->getSize());
139+
return result;
140+
}
141+
86142
static SSL_CTX* _ssl_ctx;
87143
static int _ssl_ctx_refcnt;
88144
SSL* _ssl = nullptr;
89145
int _refcnt = 0;
146+
cbuf* _rxbuf;
90147
};
91148

92149
SSL_CTX* SSLContext::_ssl_ctx = nullptr;
93150
int SSLContext::_ssl_ctx_refcnt = 0;
94151

95152

96-
WiFiClientSecure::WiFiClientSecure()
97-
{
153+
WiFiClientSecure::WiFiClientSecure() {
98154
}
99155

100-
WiFiClientSecure::~WiFiClientSecure()
101-
{
156+
WiFiClientSecure::~WiFiClientSecure() {
102157
if (_ssl) {
103158
_ssl->unref();
104159
}
@@ -164,14 +219,19 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {
164219
}
165220

166221
int WiFiClientSecure::read(uint8_t *buf, size_t size) {
222+
return _ssl->read(buf, size);
223+
}
167224

168-
uint8_t* data;
169-
int rc = ssl_read(*_ssl, &data);
170-
if (rc <= 0)
171-
return 0;
225+
int WiFiClientSecure::read() {
226+
return _ssl->read();
227+
}
228+
229+
int WiFiClientSecure::peek() {
230+
return _ssl->peek();
231+
}
172232

173-
memcpy(buf, data, rc);
174-
return rc;
233+
int WiFiClientSecure::available() {
234+
return _ssl->available();
175235
}
176236

177237
void WiFiClientSecure::stop() {
@@ -217,13 +277,13 @@ extern "C" int ax_get_file(const char *filename, uint8_t **buf) {
217277
return 0;
218278
}
219279

280+
220281
#ifdef DEBUG_TLS_MEM
221282
#define DEBUG_TLS_MEM_PRINT(...) DEBUGV(__VA_ARGS__)
222283
#else
223284
#define DEBUG_TLS_MEM_PRINT(...)
224285
#endif
225286

226-
227287
extern "C" void* ax_port_malloc(size_t size, const char* file, int line) {
228288
void* result = malloc(size);
229289

@@ -254,7 +314,6 @@ extern "C" void* ax_port_realloc(void* ptr, size_t size, const char* file, int l
254314
return result;
255315
}
256316

257-
258317
extern "C" void ax_port_free(void* ptr) {
259318
free(ptr);
260319
uint32_t *p = (uint32_t*) ptr;

libraries/ESP8266WiFi/src/WiFiClientSecure.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class WiFiClientSecure : public WiFiClient {
4040

4141
size_t write(const uint8_t *buf, size_t size) override;
4242
int read(uint8_t *buf, size_t size) override;
43+
int available() override;
44+
int read() override;
45+
int peek() override;
4346
void stop() override;
4447

4548
protected:

0 commit comments

Comments
 (0)