Skip to content

Commit 95383dd

Browse files
author
Mika Leppänen
committed
Added ipv6 support to lwip dns adaptation and updated dchp functionality
1 parent 283ee52 commit 95383dd

File tree

3 files changed

+109
-62
lines changed

3 files changed

+109
-62
lines changed

features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
#include "lwip/tcpip.h"
3030
#include "lwip/tcp.h"
3131
#include "lwip/ip.h"
32-
32+
#include "lwip/mld6.h"
33+
#include "lwip/dns.h"
34+
#include "lwip/udp.h"
3335

3436
/* Static arena of sockets */
3537
static struct lwip_socket {
@@ -94,6 +96,7 @@ static struct netif lwip_netif;
9496
static bool lwip_dhcp = false;
9597
static char lwip_mac_address[NSAPI_MAC_SIZE] = "\0";
9698

99+
#if !LWIP_IPV4 || !LWIP_IPV6
97100
static bool all_zeros(const uint8_t *p, int len)
98101
{
99102
for (int i = 0; i < len; i++) {
@@ -104,6 +107,7 @@ static bool all_zeros(const uint8_t *p, int len)
104107

105108
return true;
106109
}
110+
#endif
107111

108112
static bool convert_mbed_addr_to_lwip(ip_addr_t *out, const nsapi_addr_t *in)
109113
{
@@ -196,8 +200,8 @@ static const ip_addr_t *lwip_get_ipv6_addr(const struct netif *netif)
196200

197201
const ip_addr_t *lwip_get_ip_addr(bool any_addr, const struct netif *netif)
198202
{
199-
const ip_addr_t *pref_ip_addr;
200-
const ip_addr_t *npref_ip_addr;
203+
const ip_addr_t *pref_ip_addr = 0;
204+
const ip_addr_t *npref_ip_addr = 0;
201205

202206
#if IP_VERSION_PREF == PREF_IPV4
203207
pref_ip_addr = lwip_get_ipv4_addr(netif);
@@ -216,6 +220,37 @@ const ip_addr_t *lwip_get_ip_addr(bool any_addr, const struct netif *netif)
216220
return NULL;
217221
}
218222

223+
#if LWIP_IPV6
224+
void add_dns_addr(struct netif *lwip_netif)
225+
{
226+
const ip_addr_t *ip_addr = lwip_get_ip_addr(true, lwip_netif);
227+
if (ip_addr) {
228+
if (IP_IS_V6(ip_addr)) {
229+
const ip_addr_t *dns_ip_addr;
230+
bool dns_addr_exists = false;
231+
232+
for (char numdns = 0; numdns < DNS_MAX_SERVERS; numdns++) {
233+
dns_ip_addr = dns_getserver(numdns);
234+
if (!ip_addr_isany(dns_ip_addr)) {
235+
dns_addr_exists = true;
236+
break;
237+
}
238+
}
239+
240+
if (!dns_addr_exists) {
241+
/* 2001:4860:4860::8888 google */
242+
ip_addr_t ipv6_dns_addr = IPADDR6_INIT(
243+
PP_HTONL(0x20014860UL),
244+
PP_HTONL(0x48600000UL),
245+
PP_HTONL(0x00000000UL),
246+
PP_HTONL(0x00008888UL));
247+
dns_setserver(0, &ipv6_dns_addr);
248+
}
249+
}
250+
}
251+
}
252+
#endif
253+
219254
static sys_sem_t lwip_tcpip_inited;
220255
static void lwip_tcpip_init_irq(void *eh)
221256
{
@@ -353,28 +388,6 @@ int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
353388
// Zero out socket set
354389
lwip_arena_init();
355390

356-
// Connect to the network
357-
lwip_dhcp = dhcp;
358-
if (lwip_dhcp) {
359-
err_t err = dhcp_start(&lwip_netif);
360-
if (err) {
361-
return NSAPI_ERROR_DHCP_FAILURE;
362-
}
363-
} else {
364-
ip_addr_t ip_addr;
365-
ip_addr_t netmask_addr;
366-
ip_addr_t gw_addr;
367-
368-
if (!inet_aton(ip, &ip_addr) ||
369-
!inet_aton(netmask, &netmask_addr) ||
370-
!inet_aton(gw, &gw_addr)) {
371-
return NSAPI_ERROR_PARAMETER;
372-
}
373-
374-
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
375-
netif_set_up(&lwip_netif);
376-
}
377-
378391
#if LWIP_IPV6
379392
netif_create_ip6_linklocal_address(&lwip_netif, 1/*from MAC*/);
380393
#if LWIP_IPV6_MLD
@@ -386,7 +399,7 @@ int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
386399
if (lwip_netif.mld_mac_filter != NULL) {
387400
ip6_addr_t ip6_allnodes_ll;
388401
ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
389-
lwip_netif.mld_mac_filter(&lwip_netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
402+
lwip_netif.mld_mac_filter(&lwip_netif, &ip6_allnodes_ll, MLD6_ADD_MAC_FILTER);
390403
}
391404
#endif /* LWIP_IPV6_MLD */
392405

@@ -407,13 +420,40 @@ int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
407420
}
408421
}
409422

423+
#if LWIP_IPV4
424+
if (!dhcp) {
425+
ip4_addr_t ip_addr;
426+
ip4_addr_t netmask_addr;
427+
ip4_addr_t gw_addr;
428+
429+
if (!inet_aton(ip, &ip_addr) ||
430+
!inet_aton(netmask, &netmask_addr) ||
431+
!inet_aton(gw, &gw_addr)) {
432+
return NSAPI_ERROR_PARAMETER;
433+
}
434+
435+
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
436+
}
437+
#endif
438+
410439
netif_set_up(&lwip_netif);
411440

441+
#if LWIP_IPV4
442+
// Connect to the network
443+
lwip_dhcp = dhcp;
444+
445+
if (lwip_dhcp) {
446+
err_t err = dhcp_start(&lwip_netif);
447+
if (err) {
448+
return NSAPI_ERROR_DHCP_FAILURE;
449+
}
450+
}
451+
#endif
452+
412453
// If doesn't have address
413454
if (!lwip_get_ip_addr(true, &lwip_netif)) {
414455
//ret = sys_arch_sem_wait(&lwip_netif_has_addr, 15000);
415456
ret = sys_arch_sem_wait(&lwip_netif_has_addr, 30000);
416-
417457
if (ret == SYS_ARCH_TIMEOUT) {
418458
return NSAPI_ERROR_DHCP_FAILURE;
419459
}
@@ -428,6 +468,10 @@ int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
428468
}
429469
#endif
430470

471+
#if LWIP_IPV6
472+
add_dns_addr(&lwip_netif);
473+
#endif
474+
431475
return 0;
432476
}
433477

@@ -454,7 +498,6 @@ int lwip_bringdown(void)
454498
return 0;
455499
}
456500

457-
458501
/* LWIP error remapping */
459502
static int lwip_err_remap(err_t err) {
460503
switch (err) {
@@ -484,12 +527,27 @@ static int lwip_err_remap(err_t err) {
484527
/* LWIP network stack implementation */
485528
static int lwip_gethostbyname(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr)
486529
{
487-
err_t err = netconn_gethostbyname(host, (ip_addr_t *)addr->bytes);
530+
ip_addr_t lwip_addr;
531+
532+
#if LWIP_IPV4 && LWIP_IPV6
533+
u8_t addr_type;
534+
const ip_addr_t *ip_addr;
535+
ip_addr = lwip_get_ip_addr(true, &lwip_netif);
536+
if (IP_IS_V6(ip_addr)) {
537+
addr_type = NETCONN_DNS_IPV6;
538+
} else {
539+
addr_type = NETCONN_DNS_IPV4;
540+
}
541+
err_t err = netconn_gethostbyname_addrtype(host, &lwip_addr, addr_type);
542+
#else
543+
err_t err = netconn_gethostbyname(host, &lwip_addr);
544+
#endif
488545
if (err != ERR_OK) {
489546
return NSAPI_ERROR_DNS_FAILURE;
490547
}
491548

492-
addr->version = NSAPI_IPv4;
549+
convert_lwip_addr_to_mbed(addr, &lwip_addr);
550+
493551
return 0;
494552
}
495553

@@ -509,7 +567,7 @@ static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_
509567
u8_t lwip_proto = proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP;
510568

511569
#if LWIP_IPV6 && LWIP_IPV4
512-
ip_addr_t *ip_addr;
570+
const ip_addr_t *ip_addr;
513571
ip_addr = lwip_get_ip_addr(true, &lwip_netif);
514572

515573
if (IP_IS_V6(ip_addr)) {

features/net/network-socket/SocketAddress.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -289,22 +289,11 @@ void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16
289289
{
290290
_ip_address[0] = '\0';
291291

292-
// Check for valid IP addresses
293-
if (host && ipv4_is_valid(host)) {
294-
_addr.version = NSAPI_IPv4;
295-
ipv4_from_address(_addr.bytes, host);
296-
_port = port;
297-
} else if (host && ipv6_is_valid(host)) {
298-
_addr.version = NSAPI_IPv6;
299-
ipv6_from_address(_addr.bytes, host);
300-
_port = port;
301-
} else {
302-
// DNS lookup
303-
int err = iface->gethostbyname(host, this);
304-
_port = port;
305-
if (err) {
306-
_addr = nsapi_addr_t();
307-
_port = 0;
308-
}
292+
// gethostbyname must check for literals, so can call it directly
293+
int err = iface->gethostbyname(host, this);
294+
_port = port;
295+
if (err) {
296+
_addr = nsapi_addr_t();
297+
_port = 0;
309298
}
310299
}

features/net/network-socket/nsapi_dns.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include <stdlib.h>
2121
#include <stdio.h>
2222

23+
#define CLASS_IN 1
24+
25+
#define RR_A 1
26+
#define RR_AAAA 28
2327

2428
// DNS options
2529
#define DNS_BUFFER_SIZE 512
@@ -34,7 +38,6 @@ nsapi_addr_t dns_servers[DNS_SERVERS_SIZE] = {
3438
{NSAPI_IPv4, {208, 67, 222, 222}},
3539
};
3640

37-
3841
// DNS server configuration
3942
extern "C" int nsapi_dns_add_server(nsapi_addr_t addr)
4043
{
@@ -100,11 +103,11 @@ static void dns_append_question(uint8_t **p, const char *host, nsapi_version_t v
100103

101104
// fill out question footer
102105
if (version == NSAPI_IPv4) {
103-
dns_append_word(p, 1); // qtype = ipv4
106+
dns_append_word(p, RR_A); // qtype = ipv4
104107
} else {
105-
dns_append_word(p, 28); // qtype = ipv6
108+
dns_append_word(p, RR_AAAA); // qtype = ipv6
106109
}
107-
dns_append_word(p, 1); // qclass = 1
110+
dns_append_word(p, CLASS_IN);
108111
}
109112

110113
static int dns_scan_response(const uint8_t **p, nsapi_addr_t *addr, unsigned addr_count)
@@ -162,7 +165,7 @@ static int dns_scan_response(const uint8_t **p, nsapi_addr_t *addr, unsigned add
162165
*p += 4; // ttl
163166
uint16_t rdlength = dns_scan_word(p); // rdlength
164167

165-
if (rtype == 1 && rclass == 1 && rdlength == NSAPI_IPv4_BYTES) {
168+
if (rtype == RR_A && rclass == CLASS_IN && rdlength == NSAPI_IPv4_BYTES) {
166169
// accept A record
167170
addr->version = NSAPI_IPv4;
168171
for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
@@ -171,7 +174,7 @@ static int dns_scan_response(const uint8_t **p, nsapi_addr_t *addr, unsigned add
171174

172175
addr += 1;
173176
count += 1;
174-
} else if (rtype == 28 && rclass == 1 && rdlength == NSAPI_IPv6_BYTES) {
177+
} else if (rtype == RR_AAAA && rclass == CLASS_IN && rdlength == NSAPI_IPv6_BYTES) {
175178
// accept AAAA record
176179
addr->version = NSAPI_IPv6;
177180
for (int i = 0; i < NSAPI_IPv6_BYTES; i++) {
@@ -219,8 +222,8 @@ static int nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
219222
// check against each dns server
220223
for (unsigned i = 0; i < DNS_SERVERS_SIZE; i++) {
221224
// send the question
222-
uint8_t *p = packet;
223-
dns_append_question(&p, host, version);
225+
uint8_t *question = packet;
226+
dns_append_question(&question, host, version);
224227

225228
err = socket.sendto(SocketAddress(dns_servers[i], 53), packet, DNS_BUFFER_SIZE);
226229
if (err == NSAPI_ERROR_WOULD_BLOCK) {
@@ -239,12 +242,9 @@ static int nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
239242
break;
240243
}
241244

242-
p = packet;
243-
int found = dns_scan_response((const uint8_t **)&p, addr, addr_count);
244-
if (found) {
245-
result = found;
246-
break;
247-
}
245+
const uint8_t *response = packet;
246+
dns_scan_response(&response, addr, addr_count);
247+
break;
248248
}
249249

250250
// clean up packet

0 commit comments

Comments
 (0)