Skip to content

Commit 6f375d2

Browse files
committed
lwip - Fixed missing dns servers after bringup with static ipv4 address
Generalized handling of dns servers when brought up with both ipv4 and ipv6 addresses. Falls back to google dns servers if not dns server is found through dhcp. Also added support for the `add_dns_server` method to lwip to support custom servers.
1 parent 1465777 commit 6f375d2

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

features/FEATURE_LWIP/lwip-interface/lwip_stack.c

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -243,37 +243,43 @@ const ip_addr_t *mbed_lwip_get_ip_addr(bool any_addr, const struct netif *netif)
243243
return NULL;
244244
}
245245

246-
#if LWIP_IPV6
247246
void add_dns_addr(struct netif *lwip_netif)
248247
{
248+
// Do nothing if not brought up
249249
const ip_addr_t *ip_addr = mbed_lwip_get_ip_addr(true, lwip_netif);
250-
if (ip_addr) {
251-
if (IP_IS_V6(ip_addr)) {
252-
const ip_addr_t *dns_ip_addr;
253-
bool dns_addr_exists = false;
254-
255-
for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) {
256-
dns_ip_addr = dns_getserver(numdns);
257-
if (!ip_addr_isany(dns_ip_addr)) {
258-
dns_addr_exists = true;
259-
break;
260-
}
261-
}
250+
if (!ip_addr) {
251+
return;
252+
}
262253

263-
if (!dns_addr_exists) {
264-
/* 2001:4860:4860::8888 google */
265-
ip_addr_t ipv6_dns_addr = IPADDR6_INIT(
266-
PP_HTONL(0x20014860UL),
267-
PP_HTONL(0x48600000UL),
268-
PP_HTONL(0x00000000UL),
269-
PP_HTONL(0x00008888UL));
270-
dns_setserver(0, &ipv6_dns_addr);
271-
}
254+
// Check for existing dns server
255+
for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) {
256+
const ip_addr_t *dns_ip_addr = dns_getserver(numdns);
257+
if (!ip_addr_isany(dns_ip_addr)) {
258+
return;
272259
}
273260
}
274-
}
261+
262+
#if LWIP_IPV6
263+
if (IP_IS_V6(ip_addr)) {
264+
/* 2001:4860:4860::8888 google */
265+
ip_addr_t ipv6_dns_addr = IPADDR6_INIT(
266+
PP_HTONL(0x20014860UL),
267+
PP_HTONL(0x48600000UL),
268+
PP_HTONL(0x00000000UL),
269+
PP_HTONL(0x00008888UL));
270+
dns_setserver(0, &ipv6_dns_addr);
271+
}
275272
#endif
276273

274+
#if LWIP_IPV4
275+
if (IP_IS_V4(ip_addr)) {
276+
/* 8.8.8.8 google */
277+
ip_addr_t ipv4_dns_addr = IPADDR4_INIT(0x08080808);
278+
dns_setserver(0, &ipv4_dns_addr);
279+
}
280+
#endif
281+
}
282+
277283
static sys_sem_t lwip_tcpip_inited;
278284
static void mbed_lwip_tcpip_init_irq(void *eh)
279285
{
@@ -505,9 +511,7 @@ nsapi_error_t mbed_lwip_bringup(bool dhcp, const char *ip, const char *netmask,
505511
}
506512
#endif
507513

508-
#if LWIP_IPV6
509514
add_dns_addr(&lwip_netif);
510-
#endif
511515

512516
lwip_connected = true;
513517
return 0;
@@ -618,6 +622,22 @@ static nsapi_error_t mbed_lwip_gethostbyname(nsapi_stack_t *stack, const char *h
618622
return 0;
619623
}
620624

625+
static nsapi_error_t mbed_lwip_add_dns_server(nsapi_stack_t *stack, nsapi_addr_t addr)
626+
{
627+
// Shift all dns servers down to give precedence to new server
628+
for (int i = DNS_MAX_SERVERS-1; i > 0; i--) {
629+
dns_setserver(i, dns_getserver(i-1));
630+
}
631+
632+
ip_addr_t ip_addr;
633+
if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) {
634+
return NSAPI_ERROR_PARAMETER;
635+
}
636+
637+
dns_setserver(0, &ip_addr);
638+
return 0;
639+
}
640+
621641
static nsapi_error_t mbed_lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
622642
{
623643
// check if network is connected
@@ -874,6 +894,7 @@ static void mbed_lwip_socket_attach(nsapi_stack_t *stack, nsapi_socket_t handle,
874894
/* LWIP network stack */
875895
const nsapi_stack_api_t lwip_stack_api = {
876896
.gethostbyname = mbed_lwip_gethostbyname,
897+
.add_dns_server = mbed_lwip_add_dns_server,
877898
.socket_open = mbed_lwip_socket_open,
878899
.socket_close = mbed_lwip_socket_close,
879900
.socket_bind = mbed_lwip_socket_bind,

0 commit comments

Comments
 (0)