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,14 +1447,16 @@ static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, v ...@@ -1447,14 +1447,16 @@ 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)
{ {
if (!aResult.fromString(aHostname))
{
ip_addr_t addr; ip_addr_t addr;
aResult = static_cast<uint32_t>(0); aResult = static_cast<uint32_t>(0);
waitStatusBits(WIFI_DNS_IDLE_BIT, 16000); waitStatusBits(WIFI_DNS_IDLE_BIT, 16000);
...@@ -1470,6 +1472,7 @@ int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) ...@@ -1470,6 +1472,7 @@ int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
if((uint32_t)aResult == 0){ if((uint32_t)aResult == 0){
log_e("DNS Failed for %s", aHostname); 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