Skip to content

Fixed DNS resolution in case all sendto operations fail #7120

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

Merged
merged 1 commit into from
Jun 7, 2018
Merged
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
19 changes: 14 additions & 5 deletions features/netsocket/nsapi_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ struct DNS_QUERY {
uint8_t dns_server;
uint8_t retries;
uint8_t total_attempts;
uint8_t send_success;
uint8_t count;
dns_state state;
};

static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_t ttl);
static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t version, nsapi_addr_t *address);

static nsapi_error_t nsapi_dns_get_server_addr(NetworkStack *stack, uint8_t *index, uint8_t *total_attempts, SocketAddress *dns_addr);
static nsapi_error_t nsapi_dns_get_server_addr(NetworkStack *stack, uint8_t *index, uint8_t *total_attempts, uint8_t *send_success, SocketAddress *dns_addr);

static void nsapi_dns_query_async_create(void *ptr);
static nsapi_error_t nsapi_dns_query_async_delete(int unique_id);
Expand Down Expand Up @@ -384,7 +385,7 @@ static nsapi_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t vers
return ret_val;
}

static nsapi_error_t nsapi_dns_get_server_addr(NetworkStack *stack, uint8_t *index, uint8_t *total_attempts, SocketAddress *dns_addr)
static nsapi_error_t nsapi_dns_get_server_addr(NetworkStack *stack, uint8_t *index, uint8_t *total_attempts, uint8_t *send_success, SocketAddress *dns_addr)
{
bool dns_addr_set = false;

Expand All @@ -393,8 +394,10 @@ static nsapi_error_t nsapi_dns_get_server_addr(NetworkStack *stack, uint8_t *ind
}

if (*index >= DNS_SERVERS_SIZE + DNS_STACK_SERVERS_NUM) {
if (*total_attempts) {
// If there are total attempts left and send to has been successful at least once on this round
if (*total_attempts && *send_success) {
*index = 0;
*send_success = 0;
} else {
return NSAPI_ERROR_NO_ADDRESS;
}
Expand Down Expand Up @@ -453,11 +456,12 @@ static nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const
uint8_t retries = MBED_CONF_NSAPI_DNS_RETRIES;
uint8_t index = 0;
uint8_t total_attempts = MBED_CONF_NSAPI_DNS_TOTAL_ATTEMPTS;
uint8_t send_success = 0;

// check against each dns server
while (true) {
SocketAddress dns_addr;
err = nsapi_dns_get_server_addr(stack, &index, &total_attempts, &dns_addr);
err = nsapi_dns_get_server_addr(stack, &index, &total_attempts, &send_success, &dns_addr);
if (err != NSAPI_ERROR_OK) {
break;
}
Expand All @@ -474,6 +478,8 @@ static nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const
continue;
}

send_success++;

if (total_attempts) {
total_attempts--;
}
Expand Down Expand Up @@ -650,6 +656,7 @@ nsapi_value_or_error_t nsapi_dns_query_multiple_async(NetworkStack *stack, const
query->dns_server = 0;
query->retries = MBED_CONF_NSAPI_DNS_RETRIES + 1;
query->total_attempts = MBED_CONF_NSAPI_DNS_TOTAL_ATTEMPTS;
query->send_success = 0;
query->dns_message_id = 0;
query->socket_timeout = 0;
query->total_timeout = MBED_CONF_NSAPI_DNS_TOTAL_ATTEMPTS * MBED_CONF_NSAPI_DNS_RESPONSE_WAIT_TIME + 500;
Expand Down Expand Up @@ -971,7 +978,7 @@ static void nsapi_dns_query_async_send(void *ptr)

while (true) {
SocketAddress dns_addr;
nsapi_size_or_error_t err = nsapi_dns_get_server_addr(query->stack, &(query->dns_server), &(query->total_attempts), &dns_addr);
nsapi_size_or_error_t err = nsapi_dns_get_server_addr(query->stack, &(query->dns_server), &(query->total_attempts), &(query->send_success), &dns_addr);
if (err != NSAPI_ERROR_OK) {
nsapi_dns_query_async_resp(query, NSAPI_ERROR_TIMEOUT, NULL);
free(packet);
Expand All @@ -987,6 +994,8 @@ static void nsapi_dns_query_async_send(void *ptr)
}
}

query->send_success++;

if (query->total_attempts) {
query->total_attempts--;
}
Expand Down