Unverified Commit 05e55d87 authored by Krzysiek S's avatar Krzysiek S Committed by GitHub

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
parent 04693c66
...@@ -1447,28 +1447,31 @@ static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, v ...@@ -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. * 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 * @param aHostname Name to be resolved or string containing IP address
* @param aResult IPAddress structure to store the returned IP address * @param aResult IPAddress structure to store the returned IP address
* @return 1 if aIPAddrString was successfully converted to an IP address, * @return 1 if aIPAddrString was successfully converted to an IP address,
* else error code * else error code
*/ */
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
{ {
ip_addr_t addr; if (!aResult.fromString(aHostname))
aResult = static_cast<uint32_t>(0); {
waitStatusBits(WIFI_DNS_IDLE_BIT, 16000); ip_addr_t addr;
clearStatusBits(WIFI_DNS_IDLE_BIT | WIFI_DNS_DONE_BIT); aResult = static_cast<uint32_t>(0);
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); waitStatusBits(WIFI_DNS_IDLE_BIT, 16000);
if(err == ERR_OK && addr.u_addr.ip4.addr) { clearStatusBits(WIFI_DNS_IDLE_BIT | WIFI_DNS_DONE_BIT);
aResult = addr.u_addr.ip4.addr; err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
} else if(err == ERR_INPROGRESS) { if(err == ERR_OK && addr.u_addr.ip4.addr) {
waitStatusBits(WIFI_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s] aResult = addr.u_addr.ip4.addr;
clearStatusBits(WIFI_DNS_DONE_BIT); } else if(err == ERR_INPROGRESS) {
} waitStatusBits(WIFI_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s]
setStatusBits(WIFI_DNS_IDLE_BIT); clearStatusBits(WIFI_DNS_DONE_BIT);
if((uint32_t)aResult == 0){ }
log_e("DNS Failed for %s", aHostname); setStatusBits(WIFI_DNS_IDLE_BIT);
if((uint32_t)aResult == 0){
log_e("DNS Failed for %s", aHostname);
}
} }
return (uint32_t)aResult != 0; return (uint32_t)aResult != 0;
} }
......
...@@ -90,6 +90,8 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p ...@@ -90,6 +90,8 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
timeout = 30000; // Milli seconds. timeout = 30000; // Milli seconds.
} }
ssl_client->socket_timeout = timeout;
fd_set fdset; fd_set fdset;
struct timeval tv; struct timeval tv;
FD_ZERO(&fdset); FD_ZERO(&fdset);
...@@ -341,12 +343,15 @@ void stop_ssl_socket(sslclient_context *ssl_client, const char *rootCABuff, cons ...@@ -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_ctr_drbg_free(&ssl_client->drbg_ctx);
mbedtls_entropy_free(&ssl_client->entropy_ctx); mbedtls_entropy_free(&ssl_client->entropy_ctx);
// save only interesting field // save only interesting fields
int timeout = ssl_client->handshake_timeout; int handshake_timeout = ssl_client->handshake_timeout;
int socket_timeout = ssl_client->socket_timeout;
// reset embedded pointers to zero // reset embedded pointers to zero
memset(ssl_client, 0, sizeof(sslclient_context)); memset(ssl_client, 0, sizeof(sslclient_context));
ssl_client->handshake_timeout = timeout; ssl_client->handshake_timeout = handshake_timeout;
ssl_client->socket_timeout = socket_timeout;
} }
...@@ -369,11 +374,19 @@ int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, size_t len ...@@ -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 log_v("Writing HTTP request with %d bytes...", len); //for low level debug
int ret = -1; int ret = -1;
unsigned long write_start_time=millis();
while ((ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data, len)) <= 0) { 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) { 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 log_v("Handling error %d", ret); //for low level debug
return handle_error(ret); return handle_error(ret);
} }
//wait for space to become available //wait for space to become available
vTaskDelay(2); vTaskDelay(2);
} }
......
...@@ -24,6 +24,7 @@ typedef struct sslclient_context { ...@@ -24,6 +24,7 @@ typedef struct sslclient_context {
mbedtls_x509_crt client_cert; mbedtls_x509_crt client_cert;
mbedtls_pk_context client_key; mbedtls_pk_context client_key;
unsigned long socket_timeout;
unsigned long handshake_timeout; unsigned long handshake_timeout;
} sslclient_context; } sslclient_context;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment