Skip to content

Commit 582e643

Browse files
committed
Add proper timeout handling to WiFiClientSecure
1 parent ef07a84 commit 582e643

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

Diff for: libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ WiFiClientSecure::WiFiClientSecure()
4848
WiFiClientSecure::WiFiClientSecure(int sock)
4949
{
5050
_connected = false;
51+
_timeout = 0;
5152

5253
sslclient = new sslclient_context;
5354
ssl_init(sslclient);
@@ -98,21 +99,34 @@ int WiFiClientSecure::connect(IPAddress ip, uint16_t port)
9899
return connect(ip, port, _CA_cert, _cert, _private_key);
99100
}
100101

102+
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, int32_t timeout){
103+
_timeout = timeout;
104+
return connect(ip, port);
105+
}
106+
101107
int WiFiClientSecure::connect(const char *host, uint16_t port)
102108
{
103109
if (_pskIdent && _psKey)
104110
return connect(host, port, _pskIdent, _psKey);
105111
return connect(host, port, _CA_cert, _cert, _private_key);
106112
}
107113

114+
int WiFiClientSecure::connect(const char *host, uint16_t port, int32_t timeout){
115+
_timeout = timeout;
116+
return connect(host, port);
117+
}
118+
108119
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *_CA_cert, const char *_cert, const char *_private_key)
109120
{
110121
return connect(ip.toString().c_str(), port, _CA_cert, _cert, _private_key);
111122
}
112123

113124
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *_CA_cert, const char *_cert, const char *_private_key)
114125
{
115-
int ret = start_ssl_client(sslclient, host, port, _CA_cert, _cert, _private_key, NULL, NULL);
126+
if(_timeout > 0){
127+
sslclient->handshake_timeout = _timeout * 1000;
128+
}
129+
int ret = start_ssl_client(sslclient, host, port, _timeout, _CA_cert, _cert, _private_key, NULL, NULL);
116130
_lastError = ret;
117131
if (ret < 0) {
118132
log_e("start_ssl_client: %d", ret);
@@ -129,7 +143,10 @@ int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *pskIdent,
129143

130144
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey) {
131145
log_v("start_ssl_client with PSK");
132-
int ret = start_ssl_client(sslclient, host, port, NULL, NULL, NULL, _pskIdent, _psKey);
146+
if(_timeout > 0){
147+
sslclient->handshake_timeout = _timeout * 1000;
148+
}
149+
int ret = start_ssl_client(sslclient, host, port, _timeout, NULL, NULL, NULL, _pskIdent, _psKey);
133150
_lastError = ret;
134151
if (ret < 0) {
135152
log_e("start_ssl_client: %d", ret);

Diff for: libraries/WiFiClientSecure/src/WiFiClientSecure.h

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class WiFiClientSecure : public WiFiClient
3232

3333
int _lastError = 0;
3434
int _peek = -1;
35+
int _timeout = 0;
3536
const char *_CA_cert;
3637
const char *_cert;
3738
const char *_private_key;
@@ -44,7 +45,9 @@ class WiFiClientSecure : public WiFiClient
4445
WiFiClientSecure(int socket);
4546
~WiFiClientSecure();
4647
int connect(IPAddress ip, uint16_t port);
48+
int connect(IPAddress ip, uint16_t port, int32_t timeout);
4749
int connect(const char *host, uint16_t port);
50+
int connect(const char *host, uint16_t port, int32_t timeout);
4851
int connect(IPAddress ip, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key);
4952
int connect(const char *host, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key);
5053
int connect(IPAddress ip, uint16_t port, const char *pskIdent, const char *psKey);

Diff for: libraries/WiFiClientSecure/src/ssl_client.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ void ssl_init(sslclient_context *ssl_client)
4545
}
4646

4747

48-
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey)
48+
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, int timeout, const char *rootCABuff, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey)
4949
{
5050
char buf[512];
51-
int ret, flags, timeout;
51+
int ret, flags;
5252
int enable = 1;
5353
log_v("Free internal heap before TLS %u", ESP.getFreeHeap());
5454

@@ -73,7 +73,10 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
7373
serv_addr.sin_port = htons(port);
7474

7575
if (lwip_connect(ssl_client->socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0) {
76-
timeout = 30000;
76+
if(timeout <= 0){
77+
timeout = 30;
78+
}
79+
timeout *= 1000;//to milliseconds
7780
lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
7881
lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
7982
lwip_setsockopt(ssl_client->socket, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable));

Diff for: libraries/WiFiClientSecure/src/ssl_client.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct sslclient_context {
2929

3030

3131
void ssl_init(sslclient_context *ssl_client);
32-
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey);
32+
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, int timeout, const char *rootCABuff, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey);
3333
void stop_ssl_socket(sslclient_context *ssl_client, const char *rootCABuff, const char *cli_cert, const char *cli_key);
3434
int data_to_read(sslclient_context *ssl_client);
3535
int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, uint16_t len);

0 commit comments

Comments
 (0)