Skip to content

Commit 05e55d8

Browse files
authored
Fix unnecessary DNS query in hostByName and deadlock in ssl_client (#7351)
* Fix hostByName to avoid asking DNS when valid IP is passed via hostname param * Fix hanging in send_ssl_data
1 parent 04693c6 commit 05e55d8

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

Diff for: libraries/WiFi/src/WiFiGeneric.cpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -1447,28 +1447,31 @@ static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, v
14471447
}
14481448

14491449
/**
1450-
* Resolve the given hostname to an IP address.
1451-
* @param aHostname Name to be resolved
1450+
* Resolve the given hostname to an IP address. If passed hostname is an IP address, it will be parsed into IPAddress structure.
1451+
* @param aHostname Name to be resolved or string containing IP address
14521452
* @param aResult IPAddress structure to store the returned IP address
14531453
* @return 1 if aIPAddrString was successfully converted to an IP address,
14541454
* else error code
14551455
*/
14561456
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
14571457
{
1458-
ip_addr_t addr;
1459-
aResult = static_cast<uint32_t>(0);
1460-
waitStatusBits(WIFI_DNS_IDLE_BIT, 16000);
1461-
clearStatusBits(WIFI_DNS_IDLE_BIT | WIFI_DNS_DONE_BIT);
1462-
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
1463-
if(err == ERR_OK && addr.u_addr.ip4.addr) {
1464-
aResult = addr.u_addr.ip4.addr;
1465-
} else if(err == ERR_INPROGRESS) {
1466-
waitStatusBits(WIFI_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s]
1467-
clearStatusBits(WIFI_DNS_DONE_BIT);
1468-
}
1469-
setStatusBits(WIFI_DNS_IDLE_BIT);
1470-
if((uint32_t)aResult == 0){
1471-
log_e("DNS Failed for %s", aHostname);
1458+
if (!aResult.fromString(aHostname))
1459+
{
1460+
ip_addr_t addr;
1461+
aResult = static_cast<uint32_t>(0);
1462+
waitStatusBits(WIFI_DNS_IDLE_BIT, 16000);
1463+
clearStatusBits(WIFI_DNS_IDLE_BIT | WIFI_DNS_DONE_BIT);
1464+
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
1465+
if(err == ERR_OK && addr.u_addr.ip4.addr) {
1466+
aResult = addr.u_addr.ip4.addr;
1467+
} else if(err == ERR_INPROGRESS) {
1468+
waitStatusBits(WIFI_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s]
1469+
clearStatusBits(WIFI_DNS_DONE_BIT);
1470+
}
1471+
setStatusBits(WIFI_DNS_IDLE_BIT);
1472+
if((uint32_t)aResult == 0){
1473+
log_e("DNS Failed for %s", aHostname);
1474+
}
14721475
}
14731476
return (uint32_t)aResult != 0;
14741477
}

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
9090
timeout = 30000; // Milli seconds.
9191
}
9292

93+
ssl_client->socket_timeout = timeout;
94+
9395
fd_set fdset;
9496
struct timeval tv;
9597
FD_ZERO(&fdset);
@@ -341,12 +343,15 @@ void stop_ssl_socket(sslclient_context *ssl_client, const char *rootCABuff, cons
341343
mbedtls_ctr_drbg_free(&ssl_client->drbg_ctx);
342344
mbedtls_entropy_free(&ssl_client->entropy_ctx);
343345

344-
// save only interesting field
345-
int timeout = ssl_client->handshake_timeout;
346+
// save only interesting fields
347+
int handshake_timeout = ssl_client->handshake_timeout;
348+
int socket_timeout = ssl_client->socket_timeout;
349+
346350
// reset embedded pointers to zero
347351
memset(ssl_client, 0, sizeof(sslclient_context));
348352

349-
ssl_client->handshake_timeout = timeout;
353+
ssl_client->handshake_timeout = handshake_timeout;
354+
ssl_client->socket_timeout = socket_timeout;
350355
}
351356

352357

@@ -369,11 +374,19 @@ int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, size_t len
369374
log_v("Writing HTTP request with %d bytes...", len); //for low level debug
370375
int ret = -1;
371376

377+
unsigned long write_start_time=millis();
378+
372379
while ((ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data, len)) <= 0) {
380+
if((millis()-write_start_time)>ssl_client->socket_timeout) {
381+
log_v("SSL write timed out.");
382+
return -1;
383+
}
384+
373385
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret < 0) {
374386
log_v("Handling error %d", ret); //for low level debug
375387
return handle_error(ret);
376388
}
389+
377390
//wait for space to become available
378391
vTaskDelay(2);
379392
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef struct sslclient_context {
2424
mbedtls_x509_crt client_cert;
2525
mbedtls_pk_context client_key;
2626

27+
unsigned long socket_timeout;
2728
unsigned long handshake_timeout;
2829
} sslclient_context;
2930

0 commit comments

Comments
 (0)