Skip to content

Commit 32131b6

Browse files
author
Mika Leppänen
committed
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.
1 parent 0d5ac6e commit 32131b6

File tree

1 file changed

+69
-20
lines changed

1 file changed

+69
-20
lines changed

features/FEATURE_LWIP/lwip-interface/lwip_stack.c

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -259,39 +259,88 @@ const ip_addr_t *mbed_lwip_get_ip_addr(bool any_addr, const struct netif *netif)
259259
return NULL;
260260
}
261261

262-
void add_dns_addr(struct netif *lwip_netif)
262+
static void add_dns_addr_to_dns_list_index(const u8_t addr_type, const u8_t index)
263263
{
264-
// Do nothing if not brought up
265-
const ip_addr_t *ip_addr = mbed_lwip_get_ip_addr(true, lwip_netif);
266-
if (!ip_addr) {
267-
return;
268-
}
269-
270-
// Check for existing dns server
271-
for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) {
272-
const ip_addr_t *dns_ip_addr = dns_getserver(numdns);
273-
if (!ip_addr_isany(dns_ip_addr)) {
274-
return;
275-
}
276-
}
277-
278264
#if LWIP_IPV6
279-
if (IP_IS_V6(ip_addr)) {
265+
if (addr_type == IPADDR_TYPE_V6) {
280266
/* 2001:4860:4860::8888 google */
281267
ip_addr_t ipv6_dns_addr = IPADDR6_INIT(
282268
PP_HTONL(0x20014860UL),
283269
PP_HTONL(0x48600000UL),
284270
PP_HTONL(0x00000000UL),
285271
PP_HTONL(0x00008888UL));
286-
dns_setserver(0, &ipv6_dns_addr);
272+
dns_setserver(index, &ipv6_dns_addr);
287273
}
288274
#endif
289-
290275
#if LWIP_IPV4
291-
if (IP_IS_V4(ip_addr)) {
276+
if (addr_type == IPADDR_TYPE_V4) {
292277
/* 8.8.8.8 google */
293278
ip_addr_t ipv4_dns_addr = IPADDR4_INIT(0x08080808);
294-
dns_setserver(0, &ipv4_dns_addr);
279+
dns_setserver(index, &ipv4_dns_addr);
280+
}
281+
#endif
282+
}
283+
284+
static int get_ip_addr_type(const ip_addr_t *ip_addr)
285+
{
286+
#if LWIP_IPV6
287+
if (IP_IS_V6(ip_addr)) {
288+
return IPADDR_TYPE_V6;
289+
}
290+
#endif
291+
#if LWIP_IPV4
292+
if (IP_IS_V4(ip_addr)) {
293+
return IPADDR_TYPE_V4;
294+
}
295+
#endif
296+
return IPADDR_TYPE_ANY;
297+
}
298+
299+
void add_dns_addr(struct netif *lwip_netif)
300+
{
301+
// Check for existing dns address
302+
for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) {
303+
const ip_addr_t *dns_ip_addr = dns_getserver(numdns);
304+
if (!ip_addr_isany(dns_ip_addr)) {
305+
return;
306+
}
307+
}
308+
309+
// Get preferred ip version
310+
const ip_addr_t *ip_addr = mbed_lwip_get_ip_addr(false, lwip_netif);
311+
u8_t addr_type = IPADDR_TYPE_ANY;
312+
313+
// Add preferred ip version dns address to index 0
314+
if (ip_addr) {
315+
addr_type = get_ip_addr_type(ip_addr);
316+
add_dns_addr_to_dns_list_index(addr_type, 0);
317+
}
318+
319+
#if LWIP_IPV4 && LWIP_IPV6
320+
if (!ip_addr) {
321+
// Get address for any ip version
322+
ip_addr = mbed_lwip_get_ip_addr(true, lwip_netif);
323+
if (!ip_addr) {
324+
return;
325+
}
326+
addr_type = get_ip_addr_type(ip_addr);
327+
// Add the dns address to index 0
328+
add_dns_addr_to_dns_list_index(addr_type, 0);
329+
}
330+
331+
if (addr_type == IPADDR_TYPE_V4) {
332+
// If ipv4 is preferred and ipv6 is available add ipv6 dns address to index 1
333+
ip_addr = mbed_lwip_get_ipv6_addr(lwip_netif);
334+
} else if (addr_type == IPADDR_TYPE_V6) {
335+
// If ipv6 is preferred and ipv4 is available add ipv4 dns address to index 1
336+
ip_addr = mbed_lwip_get_ipv4_addr(lwip_netif);
337+
} else {
338+
ip_addr = NULL;
339+
}
340+
341+
if (ip_addr) {
342+
addr_type = get_ip_addr_type(ip_addr);
343+
add_dns_addr_to_dns_list_index(addr_type, 1);
295344
}
296345
#endif
297346
}

0 commit comments

Comments
 (0)