Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unnecessary DNS query in hostByName and deadlock in ssl_client #7351

Merged
merged 18 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,28 +1447,31 @@ static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, v
}

/**
* Resolve the given hostname to an IP address.
* @param aHostname Name to be resolved
* Resolve the given hostname to an IP address. If passed hostname is an IP address, it will be parsed into IPAddress structure.
* @param aHostname Name to be resolved or string containing IP address
* @param aResult IPAddress structure to store the returned IP address
* @return 1 if aIPAddrString was successfully converted to an IP address,
* else error code
*/
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
{
ip_addr_t addr;
aResult = static_cast<uint32_t>(0);
waitStatusBits(WIFI_DNS_IDLE_BIT, 16000);
clearStatusBits(WIFI_DNS_IDLE_BIT | WIFI_DNS_DONE_BIT);
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
if(err == ERR_OK && addr.u_addr.ip4.addr) {
aResult = addr.u_addr.ip4.addr;
} else if(err == ERR_INPROGRESS) {
waitStatusBits(WIFI_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s]
clearStatusBits(WIFI_DNS_DONE_BIT);
}
setStatusBits(WIFI_DNS_IDLE_BIT);
if((uint32_t)aResult == 0){
log_e("DNS Failed for %s", aHostname);
if (!aResult.fromString(aHostname))
{
ip_addr_t addr;
aResult = static_cast<uint32_t>(0);
waitStatusBits(WIFI_DNS_IDLE_BIT, 16000);
clearStatusBits(WIFI_DNS_IDLE_BIT | WIFI_DNS_DONE_BIT);
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
if(err == ERR_OK && addr.u_addr.ip4.addr) {
aResult = addr.u_addr.ip4.addr;
} else if(err == ERR_INPROGRESS) {
waitStatusBits(WIFI_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s]
clearStatusBits(WIFI_DNS_DONE_BIT);
}
setStatusBits(WIFI_DNS_IDLE_BIT);
if((uint32_t)aResult == 0){
log_e("DNS Failed for %s", aHostname);
}
}
return (uint32_t)aResult != 0;
}
Expand Down
19 changes: 16 additions & 3 deletions libraries/WiFiClientSecure/src/ssl_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
timeout = 30000; // Milli seconds.
}

ssl_client->socket_timeout = timeout;

fd_set fdset;
struct timeval tv;
FD_ZERO(&fdset);
Expand Down Expand Up @@ -341,12 +343,15 @@ void stop_ssl_socket(sslclient_context *ssl_client, const char *rootCABuff, cons
mbedtls_ctr_drbg_free(&ssl_client->drbg_ctx);
mbedtls_entropy_free(&ssl_client->entropy_ctx);

// save only interesting field
int timeout = ssl_client->handshake_timeout;
// save only interesting fields
int handshake_timeout = ssl_client->handshake_timeout;
int socket_timeout = ssl_client->socket_timeout;

// reset embedded pointers to zero
memset(ssl_client, 0, sizeof(sslclient_context));

ssl_client->handshake_timeout = timeout;
ssl_client->handshake_timeout = handshake_timeout;
ssl_client->socket_timeout = socket_timeout;
}


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

unsigned long write_start_time=millis();

while ((ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data, len)) <= 0) {
if((millis()-write_start_time)>ssl_client->socket_timeout) {
log_v("SSL write timed out.");
return -1;
}

if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret < 0) {
log_v("Handling error %d", ret); //for low level debug
return handle_error(ret);
}

//wait for space to become available
vTaskDelay(2);
}
Expand Down
1 change: 1 addition & 0 deletions libraries/WiFiClientSecure/src/ssl_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct sslclient_context {
mbedtls_x509_crt client_cert;
mbedtls_pk_context client_key;

unsigned long socket_timeout;
unsigned long handshake_timeout;
} sslclient_context;

Expand Down