Skip to content

Commit 7818fd3

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

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

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

+17-3
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);

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

+1
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);

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

+3-8
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

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

+1-1
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)