Improved manual DNS address addition for dual stack case

If DHCP or PPP does not provide DNS addresses, for dual stack, adds both ipv4
and ipv6 DNS addresses to DNS list.
pull/4911/head
Mika Leppänen 2017-08-17 11:58:47 +03:00
parent 0d5ac6e78d
commit 32131b61a8
1 changed files with 72 additions and 23 deletions

View File

@ -259,15 +259,46 @@ const ip_addr_t *mbed_lwip_get_ip_addr(bool any_addr, const struct netif *netif)
return NULL; return NULL;
} }
void add_dns_addr(struct netif *lwip_netif) static void add_dns_addr_to_dns_list_index(const u8_t addr_type, const u8_t index)
{ {
// Do nothing if not brought up #if LWIP_IPV6
const ip_addr_t *ip_addr = mbed_lwip_get_ip_addr(true, lwip_netif); if (addr_type == IPADDR_TYPE_V6) {
if (!ip_addr) { /* 2001:4860:4860::8888 google */
return; ip_addr_t ipv6_dns_addr = IPADDR6_INIT(
PP_HTONL(0x20014860UL),
PP_HTONL(0x48600000UL),
PP_HTONL(0x00000000UL),
PP_HTONL(0x00008888UL));
dns_setserver(index, &ipv6_dns_addr);
}
#endif
#if LWIP_IPV4
if (addr_type == IPADDR_TYPE_V4) {
/* 8.8.8.8 google */
ip_addr_t ipv4_dns_addr = IPADDR4_INIT(0x08080808);
dns_setserver(index, &ipv4_dns_addr);
}
#endif
} }
// Check for existing dns server static int get_ip_addr_type(const ip_addr_t *ip_addr)
{
#if LWIP_IPV6
if (IP_IS_V6(ip_addr)) {
return IPADDR_TYPE_V6;
}
#endif
#if LWIP_IPV4
if (IP_IS_V4(ip_addr)) {
return IPADDR_TYPE_V4;
}
#endif
return IPADDR_TYPE_ANY;
}
void add_dns_addr(struct netif *lwip_netif)
{
// Check for existing dns address
for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) { for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) {
const ip_addr_t *dns_ip_addr = dns_getserver(numdns); const ip_addr_t *dns_ip_addr = dns_getserver(numdns);
if (!ip_addr_isany(dns_ip_addr)) { if (!ip_addr_isany(dns_ip_addr)) {
@ -275,23 +306,41 @@ void add_dns_addr(struct netif *lwip_netif)
} }
} }
#if LWIP_IPV6 // Get preferred ip version
if (IP_IS_V6(ip_addr)) { const ip_addr_t *ip_addr = mbed_lwip_get_ip_addr(false, lwip_netif);
/* 2001:4860:4860::8888 google */ u8_t addr_type = IPADDR_TYPE_ANY;
ip_addr_t ipv6_dns_addr = IPADDR6_INIT(
PP_HTONL(0x20014860UL),
PP_HTONL(0x48600000UL),
PP_HTONL(0x00000000UL),
PP_HTONL(0x00008888UL));
dns_setserver(0, &ipv6_dns_addr);
}
#endif
#if LWIP_IPV4 // Add preferred ip version dns address to index 0
if (IP_IS_V4(ip_addr)) { if (ip_addr) {
/* 8.8.8.8 google */ addr_type = get_ip_addr_type(ip_addr);
ip_addr_t ipv4_dns_addr = IPADDR4_INIT(0x08080808); add_dns_addr_to_dns_list_index(addr_type, 0);
dns_setserver(0, &ipv4_dns_addr); }
#if LWIP_IPV4 && LWIP_IPV6
if (!ip_addr) {
// Get address for any ip version
ip_addr = mbed_lwip_get_ip_addr(true, lwip_netif);
if (!ip_addr) {
return;
}
addr_type = get_ip_addr_type(ip_addr);
// Add the dns address to index 0
add_dns_addr_to_dns_list_index(addr_type, 0);
}
if (addr_type == IPADDR_TYPE_V4) {
// If ipv4 is preferred and ipv6 is available add ipv6 dns address to index 1
ip_addr = mbed_lwip_get_ipv6_addr(lwip_netif);
} else if (addr_type == IPADDR_TYPE_V6) {
// If ipv6 is preferred and ipv4 is available add ipv4 dns address to index 1
ip_addr = mbed_lwip_get_ipv4_addr(lwip_netif);
} else {
ip_addr = NULL;
}
if (ip_addr) {
addr_type = get_ip_addr_type(ip_addr);
add_dns_addr_to_dns_list_index(addr_type, 1);
} }
#endif #endif
} }