29
29
#include "lwip/tcpip.h"
30
30
#include "lwip/tcp.h"
31
31
#include "lwip/ip.h"
32
-
32
+ #include "lwip/mld6.h"
33
+ #include "lwip/dns.h"
34
+ #include "lwip/udp.h"
33
35
34
36
/* Static arena of sockets */
35
37
static struct lwip_socket {
@@ -94,6 +96,7 @@ static struct netif lwip_netif;
94
96
static bool lwip_dhcp = false;
95
97
static char lwip_mac_address [NSAPI_MAC_SIZE ] = "\0" ;
96
98
99
+ #if !LWIP_IPV4 || !LWIP_IPV6
97
100
static bool all_zeros (const uint8_t * p , int len )
98
101
{
99
102
for (int i = 0 ; i < len ; i ++ ) {
@@ -104,6 +107,7 @@ static bool all_zeros(const uint8_t *p, int len)
104
107
105
108
return true;
106
109
}
110
+ #endif
107
111
108
112
static bool convert_mbed_addr_to_lwip (ip_addr_t * out , const nsapi_addr_t * in )
109
113
{
@@ -196,8 +200,8 @@ static const ip_addr_t *lwip_get_ipv6_addr(const struct netif *netif)
196
200
197
201
const ip_addr_t * lwip_get_ip_addr (bool any_addr , const struct netif * netif )
198
202
{
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 ;
201
205
202
206
#if IP_VERSION_PREF == PREF_IPV4
203
207
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)
216
220
return NULL ;
217
221
}
218
222
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
+
219
254
static sys_sem_t lwip_tcpip_inited ;
220
255
static void lwip_tcpip_init_irq (void * eh )
221
256
{
@@ -353,28 +388,6 @@ int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
353
388
// Zero out socket set
354
389
lwip_arena_init ();
355
390
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
-
378
391
#if LWIP_IPV6
379
392
netif_create_ip6_linklocal_address (& lwip_netif , 1 /*from MAC*/ );
380
393
#if LWIP_IPV6_MLD
@@ -386,7 +399,7 @@ int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
386
399
if (lwip_netif .mld_mac_filter != NULL ) {
387
400
ip6_addr_t ip6_allnodes_ll ;
388
401
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 );
390
403
}
391
404
#endif /* LWIP_IPV6_MLD */
392
405
@@ -407,13 +420,40 @@ int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
407
420
}
408
421
}
409
422
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
+
410
439
netif_set_up (& lwip_netif );
411
440
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
+
412
453
// If doesn't have address
413
454
if (!lwip_get_ip_addr (true, & lwip_netif )) {
414
455
//ret = sys_arch_sem_wait(&lwip_netif_has_addr, 15000);
415
456
ret = sys_arch_sem_wait (& lwip_netif_has_addr , 30000 );
416
-
417
457
if (ret == SYS_ARCH_TIMEOUT ) {
418
458
return NSAPI_ERROR_DHCP_FAILURE ;
419
459
}
@@ -428,6 +468,10 @@ int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
428
468
}
429
469
#endif
430
470
471
+ #if LWIP_IPV6
472
+ add_dns_addr (& lwip_netif );
473
+ #endif
474
+
431
475
return 0 ;
432
476
}
433
477
@@ -454,7 +498,6 @@ int lwip_bringdown(void)
454
498
return 0 ;
455
499
}
456
500
457
-
458
501
/* LWIP error remapping */
459
502
static int lwip_err_remap (err_t err ) {
460
503
switch (err ) {
@@ -484,12 +527,27 @@ static int lwip_err_remap(err_t err) {
484
527
/* LWIP network stack implementation */
485
528
static int lwip_gethostbyname (nsapi_stack_t * stack , const char * host , nsapi_addr_t * addr )
486
529
{
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
488
545
if (err != ERR_OK ) {
489
546
return NSAPI_ERROR_DNS_FAILURE ;
490
547
}
491
548
492
- addr -> version = NSAPI_IPv4 ;
549
+ convert_lwip_addr_to_mbed (addr , & lwip_addr );
550
+
493
551
return 0 ;
494
552
}
495
553
@@ -509,7 +567,7 @@ static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_
509
567
u8_t lwip_proto = proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP ;
510
568
511
569
#if LWIP_IPV6 && LWIP_IPV4
512
- ip_addr_t * ip_addr ;
570
+ const ip_addr_t * ip_addr ;
513
571
ip_addr = lwip_get_ip_addr (true, & lwip_netif );
514
572
515
573
if (IP_IS_V6 (ip_addr )) {
0 commit comments