Skip to content

Commit a3a1e7e

Browse files
cziter15me-no-dev
authored andcommitted
Allow passing IP as connect method parameter in WiFiClientSecure and skip unnecessary host-ip conversions (#7643)
1 parent a0da3c1 commit a3a1e7e

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
@@ -53,7 +53,7 @@ void ssl_init(sslclient_context *ssl_client)
5353
mbedtls_ctr_drbg_init(&ssl_client->drbg_ctx);
5454
}
5555

56-
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)
56+
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)
5757
{
5858
char buf[512];
5959
int ret, flags;
@@ -73,16 +73,11 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
7373
return ssl_client->socket;
7474
}
7575

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

8883
if(timeout <= 0){
@@ -261,7 +256,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
261256
log_v("Setting hostname for TLS session...");
262257

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

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)