Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7818fd3

Browse filesBrowse files
authored
Allow passing IP as connect method parameter in WiFiClientSecure and skip unnecessary host-ip conversions (espressif#7643)
1 parent 1e1dd8b commit 7818fd3
Copy full SHA for 7818fd3

File tree

4 files changed

+22
-12
lines changed
Filter options

4 files changed

+22
-12
lines changed

‎libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

Copy file name to clipboardExpand all lines: libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
+17-3Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,21 @@ int WiFiClientSecure::connect(const char *host, uint16_t port, int32_t timeout){
124124

125125
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *CA_cert, const char *cert, const char *private_key)
126126
{
127-
return connect(ip.toString().c_str(), port, CA_cert, cert, private_key);
127+
return connect(ip, port, NULL, CA_cert, cert, private_key);
128128
}
129129

130130
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *CA_cert, const char *cert, const char *private_key)
131131
{
132-
int ret = start_ssl_client(sslclient, host, port, _timeout, CA_cert, _use_ca_bundle, cert, private_key, NULL, NULL, _use_insecure, _alpn_protos);
132+
IPAddress address;
133+
if (!WiFi.hostByName(host, address))
134+
return 0;
135+
136+
return connect(address, port, host, CA_cert, cert, private_key);
137+
}
138+
139+
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *host, const char *CA_cert, const char *cert, const char *private_key)
140+
{
141+
int ret = start_ssl_client(sslclient, ip, port, host, _timeout, CA_cert, _use_ca_bundle, cert, private_key, NULL, NULL, _use_insecure, _alpn_protos);
133142
_lastError = ret;
134143
if (ret < 0) {
135144
log_e("start_ssl_client: %d", ret);
@@ -146,7 +155,12 @@ int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *pskIdent,
146155

147156
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey) {
148157
log_v("start_ssl_client with PSK");
149-
int ret = start_ssl_client(sslclient, host, port, _timeout, NULL, false, NULL, NULL, pskIdent, psKey, _use_insecure, _alpn_protos);
158+
159+
IPAddress address;
160+
if (!WiFi.hostByName(host, address))
161+
return 0;
162+
163+
int ret = start_ssl_client(sslclient, address, port, host, _timeout, NULL, false, NULL, NULL, pskIdent, psKey, _use_insecure, _alpn_protos);
150164
_lastError = ret;
151165
if (ret < 0) {
152166
log_e("start_ssl_client: %d", ret);

‎libraries/WiFiClientSecure/src/WiFiClientSecure.h

Copy file name to clipboardExpand all lines: libraries/WiFiClientSecure/src/WiFiClientSecure.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class WiFiClientSecure : public WiFiClient
5555
int connect(const char *host, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key);
5656
int connect(IPAddress ip, uint16_t port, const char *pskIdent, const char *psKey);
5757
int connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey);
58+
int connect(IPAddress ip, uint16_t port, const char *host, const char *CA_cert, const char *cert, const char *private_key);
5859
int peek();
5960
size_t write(uint8_t data);
6061
size_t write(const uint8_t *buf, size_t size);

‎libraries/WiFiClientSecure/src/ssl_client.cpp

Copy file name to clipboardExpand all lines: libraries/WiFiClientSecure/src/ssl_client.cpp
+3-8Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void ssl_init(sslclient_context *ssl_client)
5454
}
5555

5656

57-
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, int timeout, const char *rootCABuff, bool useRootCABundle, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey, bool insecure, const char **alpn_protos)
57+
int start_ssl_client(sslclient_context *ssl_client, const IPAddress& ip, uint32_t port, const char* hostname, int timeout, const char *rootCABuff, bool useRootCABundle, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey, bool insecure, const char **alpn_protos)
5858
{
5959
char buf[512];
6060
int ret, flags;
@@ -74,16 +74,11 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
7474
return ssl_client->socket;
7575
}
7676

77-
IPAddress srv((uint32_t)0);
78-
if(!WiFiGenericClass::hostByName(host, srv)){
79-
return -1;
80-
}
81-
8277
fcntl( ssl_client->socket, F_SETFL, fcntl( ssl_client->socket, F_GETFL, 0 ) | O_NONBLOCK );
8378
struct sockaddr_in serv_addr;
8479
memset(&serv_addr, 0, sizeof(serv_addr));
8580
serv_addr.sin_family = AF_INET;
86-
serv_addr.sin_addr.s_addr = srv;
81+
serv_addr.sin_addr.s_addr = ip;
8782
serv_addr.sin_port = htons(port);
8883

8984
if(timeout <= 0){
@@ -259,7 +254,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
259254
log_v("Setting hostname for TLS session...");
260255

261256
// Hostname set here should match CN in server certificate
262-
if((ret = mbedtls_ssl_set_hostname(&ssl_client->ssl_ctx, host)) != 0){
257+
if((ret = mbedtls_ssl_set_hostname(&ssl_client->ssl_ctx, hostname != NULL ? hostname : ip.toString().c_str())) != 0){
263258
return handle_error(ret);
264259
}
265260

‎libraries/WiFiClientSecure/src/ssl_client.h

Copy file name to clipboardExpand all lines: libraries/WiFiClientSecure/src/ssl_client.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct sslclient_context {
3030

3131

3232
void ssl_init(sslclient_context *ssl_client);
33-
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, int timeout, const char *rootCABuff, bool useRootCABundle, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey, bool insecure, const char **alpn_protos);
33+
int start_ssl_client(sslclient_context *ssl_client, const IPAddress& ip, uint32_t port, const char* hostname, int timeout, const char *rootCABuff, bool useRootCABundle, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey, bool insecure, const char **alpn_protos);
3434
void stop_ssl_socket(sslclient_context *ssl_client, const char *rootCABuff, const char *cli_cert, const char *cli_key);
3535
int data_to_read(sslclient_context *ssl_client);
3636
int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, size_t len);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.