Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit f472595

Browse files
author
Me No Dev
committed
ssl optimizations
1 parent a627ef4 commit f472595

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

src/ESPAsyncTCP.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ extern "C"{
3030
}
3131
#include <tcp_axtls.h>
3232

33-
34-
bool AsyncServer::_ssl_hasClient = false;
35-
3633
/*
3734
Async TCP Client
3835
*/
@@ -233,7 +230,7 @@ size_t AsyncClient::add(const char* data, size_t size) {
233230
int sent = tcp_ssl_write(_pcb, (uint8_t*)data, size);
234231
if(sent >= 0)
235232
return sent;
236-
//ssl error
233+
_close();
237234
return 0;
238235
}
239236
size_t will_send = (room < size) ? room : size;
@@ -294,9 +291,6 @@ int8_t AsyncClient::_close(){
294291
int8_t err = ERR_OK;
295292
if(_pcb) {
296293
if(_pcb_secure){
297-
if(tcp_ssl_is_server(_pcb) == 1 && AsyncServer::_ssl_hasClient){
298-
AsyncServer::_ssl_hasClient = false;
299-
}
300294
tcp_ssl_free(_pcb);
301295
}
302296
tcp_arg(_pcb, NULL);
@@ -318,9 +312,6 @@ int8_t AsyncClient::_close(){
318312
void AsyncClient::_error(int8_t err) {
319313
if(_pcb){
320314
if(_pcb_secure){
321-
if(tcp_ssl_is_server(_pcb) == 1 && AsyncServer::_ssl_hasClient){
322-
AsyncServer::_ssl_hasClient = false;
323-
}
324315
tcp_ssl_free(_pcb);
325316
}
326317
tcp_arg(_pcb, NULL);
@@ -361,6 +352,7 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
361352
int read_bytes = tcp_ssl_read(pcb, pb);
362353
if(read_bytes < 0){
363354
if (read_bytes != SSL_CLOSE_NOTIFY) {
355+
ets_printf("_recv err: %d\n", read_bytes);
364356
_close();
365357
}
366358
return read_bytes;
@@ -393,6 +385,7 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
393385
return ERR_OK;
394386
}
395387
uint32_t now = millis();
388+
396389
// ACK Timeout
397390
if(_pcb_busy && _ack_timeout && (now - _pcb_sent_at) >= _ack_timeout){
398391
_pcb_busy = false;
@@ -401,10 +394,16 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
401394
return ERR_OK;
402395
}
403396
// RX Timeout
404-
if(_rx_since_timeout && now - _rx_last_packet >= _rx_since_timeout * 1000){
397+
if(_rx_since_timeout && (now - _rx_last_packet) >= (_rx_since_timeout * 1000)){
405398
_close();
406399
return ERR_OK;
407400
}
401+
// SSL Handshake Timeout
402+
if(_pcb_secure && !_handshake_done && (now - _rx_last_packet) >= 2000){
403+
_close();
404+
return ERR_OK;
405+
}
406+
408407
// Everything is fine
409408
if(_poll_cb)
410409
_poll_cb(_poll_cb_arg, this);
@@ -711,7 +710,6 @@ AsyncServer::AsyncServer(IPAddress addr, uint16_t port)
711710
, _pcb(0)
712711
, _pending(NULL)
713712
, _ssl_ctx(NULL)
714-
, _clients_waiting(0)
715713
, _connect_cb(0)
716714
, _connect_cb_arg(0)
717715
, _file_cb(0)
@@ -725,7 +723,6 @@ AsyncServer::AsyncServer(uint16_t port)
725723
, _pcb(0)
726724
, _pending(NULL)
727725
, _ssl_ctx(NULL)
728-
, _clients_waiting(0)
729726
, _connect_cb(0)
730727
, _connect_cb_arg(0)
731728
, _file_cb(0)
@@ -833,7 +830,7 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
833830
tcp_nagle_enable(pcb);
834831

835832
if(_ssl_ctx){
836-
if(_ssl_hasClient || _pending){
833+
if(tcp_ssl_has_client() || _pending){
837834
struct pending_pcb * new_item = (struct pending_pcb*)malloc(sizeof(struct pending_pcb));
838835
if(!new_item){
839836
//ets_printf("### malloc new pending failed!\n");
@@ -842,7 +839,6 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
842839
}
843840
return ERR_OK;
844841
}
845-
_clients_waiting++;
846842
//ets_printf("### put to wait: %d\n", _clients_waiting);
847843
new_item->pcb = pcb;
848844
new_item->pb = NULL;
@@ -863,7 +859,6 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
863859
} else {
864860
AsyncClient *c = new AsyncClient(pcb, _ssl_ctx);
865861
if(c){
866-
_ssl_hasClient = true;
867862
c->onConnect([this](void * arg, AsyncClient *c){
868863
_connect_cb(_connect_cb_arg, c);
869864
}, this);
@@ -885,7 +880,7 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
885880
}
886881

887882
int8_t AsyncServer::_poll(tcp_pcb* pcb){
888-
if(!_ssl_hasClient && _pending){
883+
if(!tcp_ssl_has_client() && _pending){
889884
struct pending_pcb * p = _pending;
890885
if(p->pcb == pcb){
891886
_pending = _pending->next;
@@ -896,8 +891,6 @@ int8_t AsyncServer::_poll(tcp_pcb* pcb){
896891
p->next = b->next;
897892
p = b;
898893
}
899-
_ssl_hasClient = true;
900-
_clients_waiting--;
901894
//ets_printf("### remove from wait: %d\n", _clients_waiting);
902895
AsyncClient *c = new AsyncClient(pcb, _ssl_ctx);
903896
if(c){
@@ -919,7 +912,6 @@ int8_t AsyncServer::_recv(struct tcp_pcb *pcb, struct pbuf *pb, int8_t err){
919912
struct pending_pcb * p;
920913

921914
if(!pb){
922-
_clients_waiting--;
923915
//ets_printf("### close from wait: %d\n", _clients_waiting);
924916
p = _pending;
925917
if(p->pcb == pcb){

src/ESPAsyncTCP.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,12 @@ class AsyncServer {
180180
tcp_pcb* _pcb;
181181
struct pending_pcb * _pending;
182182
SSL_CTX * _ssl_ctx;
183-
size_t _clients_waiting;
184183
AcConnectHandler _connect_cb;
185184
void* _connect_cb_arg;
186185
AcSSlFileHandler _file_cb;
187186
void* _file_cb_arg;
188187

189188
public:
190-
static bool _ssl_hasClient;
191189

192190
AsyncServer(IPAddress addr, uint16_t port);
193191
AsyncServer(uint16_t port);

src/tcp_axtls.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ uint16_t default_private_key_len = 0;
3737
uint8_t * default_certificate = NULL;
3838
uint16_t default_certificate_len = 0;
3939

40+
static uint8_t _tcp_ssl_has_client = 0;
41+
4042
SSL_CTX * tcp_ssl_new_server_ctx(const char *cert, const char *private_key_file, const char *password){
4143
uint32_t options = SSL_CONNECT_IN_PARTS;
4244
SSL_CTX *ssl_ctx;
@@ -94,6 +96,10 @@ typedef struct tcp_ssl_pcb tcp_ssl_t;
9496
static tcp_ssl_t * tcp_ssl_array = NULL;
9597
static int tcp_ssl_next_fd = 0;
9698

99+
uint8_t tcp_ssl_has_client(){
100+
return _tcp_ssl_has_client;
101+
}
102+
97103
tcp_ssl_t * tcp_ssl_new(struct tcp_pcb *tcp) {
98104

99105
if(tcp_ssl_next_fd < 0){
@@ -129,7 +135,7 @@ tcp_ssl_t * tcp_ssl_new(struct tcp_pcb *tcp) {
129135
item->next = new_item;
130136
}
131137

132-
//TCP_SSL_DEBUG("tcp_ssl_new: %d\n", new_item->fd);
138+
TCP_SSL_DEBUG("tcp_ssl_new: %d\n", new_item->fd);
133139
return new_item;
134140
}
135141

@@ -205,6 +211,7 @@ int tcp_ssl_new_server(struct tcp_pcb *tcp, SSL_CTX* ssl_ctx){
205211
tcp_ssl->type = TCP_SSL_TYPE_SERVER;
206212
tcp_ssl->ssl_ctx = ssl_ctx;
207213

214+
_tcp_ssl_has_client = 1;
208215
tcp_ssl->ssl = ssl_server_new(ssl_ctx, tcp_ssl->fd);
209216
if(tcp_ssl->ssl == NULL){
210217
TCP_SSL_DEBUG("tcp_ssl_new_server: failed to allocate ssl\n");
@@ -228,11 +235,13 @@ int tcp_ssl_free(struct tcp_pcb *tcp) {
228235
if(item->tcp_pbuf != NULL){
229236
pbuf_free(item->tcp_pbuf);
230237
}
231-
//TCP_SSL_DEBUG("tcp_ssl_free: %d\n", item->fd);
238+
TCP_SSL_DEBUG("tcp_ssl_free: %d\n", item->fd);
232239
if(item->ssl)
233240
ssl_free(item->ssl);
234241
if(item->type == TCP_SSL_TYPE_CLIENT && item->ssl_ctx)
235242
ssl_ctx_free(item->ssl_ctx);
243+
if(item->type == TCP_SSL_TYPE_SERVER)
244+
_tcp_ssl_has_client = 0;
236245
free(item);
237246
return 0;
238247
}
@@ -249,13 +258,14 @@ int tcp_ssl_free(struct tcp_pcb *tcp) {
249258
if(i->tcp_pbuf != NULL){
250259
pbuf_free(i->tcp_pbuf);
251260
}
252-
//TCP_SSL_DEBUG("tcp_ssl_free: %d\n", i->fd);
261+
TCP_SSL_DEBUG("tcp_ssl_free: %d\n", i->fd);
253262
if(i->ssl)
254263
ssl_free(i->ssl);
255264
if(i->type == TCP_SSL_TYPE_CLIENT && i->ssl_ctx)
256265
ssl_ctx_free(i->ssl_ctx);
266+
if(i->type == TCP_SSL_TYPE_SERVER)
267+
_tcp_ssl_has_client = 0;
257268
free(i);
258-
259269
return 0;
260270
}
261271

src/tcp_axtls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ typedef void (* tcp_ssl_handshake_cb_t)(void *arg, struct tcp_pcb *tcp, SSL *ssl
6363
typedef void (* tcp_ssl_error_cb_t)(void *arg, struct tcp_pcb *tcp, int8_t error);
6464
typedef int (* tcp_ssl_file_cb_t)(void *arg, const char *filename, uint8_t **buf);
6565

66+
uint8_t tcp_ssl_has_client();
67+
6668
int tcp_ssl_new_client(struct tcp_pcb *tcp);
6769

6870
SSL_CTX * tcp_ssl_new_server_ctx(const char *cert, const char *private_key_file, const char *password);

0 commit comments

Comments
 (0)