Skip to content

Commit 42b5680

Browse files
tymoteuszblochmobicaCruz Monrreal II
authored andcommitted
Multihoming fixed according to new LWIP implementation.
1 parent 1632cc9 commit 42b5680

File tree

16 files changed

+214
-53
lines changed

16 files changed

+214
-53
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ static void test_failure_handler(const failure_t failure)
194194

195195

196196
Case cases[] = {
197+
Case("TCPSOCKET_OPEN_LIMIT", TCPSOCKET_OPEN_LIMIT),
197198
Case("TCPSOCKET_ECHOTEST", TCPSOCKET_ECHOTEST),
198199
Case("TCPSOCKET_ECHOTEST_NONBLOCK", TCPSOCKET_ECHOTEST_NONBLOCK),
199200
Case("TCPSOCKET_OPEN_CLOSE_REPEAT", TCPSOCKET_OPEN_CLOSE_REPEAT),
200-
Case("TCPSOCKET_OPEN_LIMIT", TCPSOCKET_OPEN_LIMIT),
201201
Case("TCPSOCKET_THREAD_PER_SOCKET_SAFETY", TCPSOCKET_THREAD_PER_SOCKET_SAFETY),
202202
Case("TCPSOCKET_CONNECT_INVALID", TCPSOCKET_CONNECT_INVALID),
203203
Case("TCPSOCKET_ECHOTEST_BURST", TCPSOCKET_ECHOTEST_BURST),

TESTS/network/multihoming/main.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#error [NOT_SUPPORTED] Both Wifi and Ethernet devices are required for multihoming tests.
3030
#endif
3131

32+
#define STRING_VERIFY(str) (str != NULL ? str : "not supported")
33+
3234

3335
#include "greentea-client/test_env.h"
3436
#include "unity/unity.h"
@@ -95,13 +97,12 @@ static void _ifup()
9597
TEST_FAIL_MESSAGE("Wifi connection error!");
9698
return;
9799
}
98-
wifi->get_interface_name(interface_name[interface_num]);
99-
printf("MAC: %s\n", wifi->get_mac_address());
100-
printf("IP: %s\n", wifi->get_ip_address());
101-
printf("Netmask: %s\n", wifi->get_netmask());
102-
printf("Gateway: %s\n", wifi->get_gateway());
100+
printf("Wifi interface name: %s\n\n", STRING_VERIFY(wifi->get_interface_name(interface_name[interface_num])));
101+
printf("MAC: %s\n", STRING_VERIFY(wifi->get_mac_address()));
102+
printf("IP: %s\n", STRING_VERIFY(wifi->get_ip_address()));
103+
printf("Netmask: %s\n", STRING_VERIFY(wifi->get_netmask()));
104+
printf("Gateway: %s\n", STRING_VERIFY(wifi->get_gateway()));
103105
printf("RSSI: %d\n\n", wifi->get_rssi());
104-
printf("Wifi interface name: %s\n\n", interface_name[interface_num]);
105106
interface_num++;
106107
} else {
107108
TEST_FAIL_MESSAGE("ERROR: No WiFiInterface found!");

features/lwipstack/.mbedignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ lwip/test/*
33
lwip/src/apps/*
44
lwip/src/netif/lwip_slipif.c
55
lwip/src/include/lwip/apps/*
6-
lwip/src/include/posix/*
6+
lwip/src/include/compat/stdc/*
77
lwip-eth/*

features/lwipstack/LWIPStack.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -514,19 +514,9 @@ nsapi_error_t LWIP::setsockopt(nsapi_socket_t handle, int level, int optname, co
514514
if (optlen > NSAPI_INTERFACE_NAME_MAX_SIZE) {
515515
return NSAPI_ERROR_UNSUPPORTED;
516516
}
517-
#if LWIP_TCP
518-
if (NETCONNTYPE_GROUP(s->conn->type) == NETCONN_TCP) {
519-
s->conn->pcb.tcp->interface_name = (const char *)optval;
520-
}
521-
#endif
522-
if (NETCONNTYPE_GROUP(s->conn->type) == NETCONN_UDP) {
523-
s->conn->pcb.udp->interface_name = (const char *)optval;
524-
}
525-
#if LWIP_RAW
526-
if (NETCONNTYPE_GROUP(s->conn->type) == NETCONN_RAW) {
527-
s->conn->pcb.raw->interface_name = (const char *)optval;
528-
}
529-
#endif
517+
518+
netconn_bind_if(s->conn, netif_name_to_index((const char *)optval));
519+
530520
return 0;
531521
#if LWIP_TCP
532522
case NSAPI_KEEPALIVE:

features/lwipstack/lwip/src/core/ipv4/lwip_dhcp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ dhcp_handle_ack(struct netif *netif, struct dhcp_msg *msg_in)
673673
for (n = 0; (n < LWIP_DHCP_PROVIDE_DNS_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n); n++) {
674674
ip_addr_t dns_addr;
675675
ip_addr_set_ip4_u32_val(dns_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
676-
dns_setserver(n, &dns_addr);
676+
dns_setserver(n, &dns_addr,netif);
677677
}
678678
#endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
679679
}

features/lwipstack/lwip/src/core/ipv6/lwip_ip6.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest)
9696

9797
/* If single netif configuration, fast return. */
9898
if ((netif_list != NULL) && (netif_list->next == NULL)) {
99-
if (!netif_is_up(netif_list) || !netif_is_link_up(netif_list)){//||
100-
// (ip6_addr_has_zone(dest) && !ip6_addr_test_zone(dest, netif_list))) {
99+
if (!netif_is_up(netif_list) || !netif_is_link_up(netif_list) ||
100+
(ip6_addr_has_zone(dest) && !ip6_addr_test_zone(dest, netif_list))) {
101101
return NULL;
102102
}
103103
return netif_list;

features/lwipstack/lwip/src/core/ipv6/lwip_nd6.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,14 +776,14 @@ nd6_input(struct pbuf *p, struct netif *inp)
776776

777777
if (htonl(rdnss_opt->lifetime) > 0) {
778778
/* TODO implement Lifetime > 0 */
779-
dns_setserver(rdnss_server_idx++, &rdnss_address);
779+
dns_setserver(rdnss_server_idx++, &rdnss_address, inp);
780780
} else {
781781
/* TODO implement DNS removal in dns.c */
782782
u8_t s;
783783
for (s = 0; s < DNS_MAX_SERVERS; s++) {
784-
const ip_addr_t *addr = dns_getserver(s);
784+
const ip_addr_t *addr = dns_getserver(s,netif_get_name(inp));
785785
if(ip_addr_cmp(addr, &rdnss_address)) {
786-
dns_setserver(s, NULL);
786+
dns_setserver(s, NULL, inp);
787787
}
788788
}
789789
}

features/lwipstack/lwip/src/core/lwip_dns.c

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ static struct dns_table_entry dns_table[DNS_TABLE_SIZE];
302302
static struct dns_req_entry dns_requests[DNS_MAX_REQUESTS];
303303
#endif
304304
static ip_addr_t dns_servers[DNS_MAX_SERVERS];
305+
struct dns_server_interface *multihoming_dns_servers;
305306

306307
#if LWIP_IPV4
307308
const ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT;
@@ -321,7 +322,7 @@ dns_init(void)
321322
/* initialize default DNS server address */
322323
ip_addr_t dnsserver;
323324
DNS_SERVER_ADDRESS(&dnsserver);
324-
dns_setserver(0, &dnsserver);
325+
dns_setserver(0, &dnsserver, NULL);
325326
#endif /* DNS_SERVER_ADDRESS */
326327

327328
#if LWIP_FULL_DNS
@@ -363,15 +364,114 @@ dns_init(void)
363364
* @param dnsserver IP address of the DNS server to set
364365
*/
365366
void
366-
dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
367+
dns_setserver(u8_t numdns, const ip_addr_t *dnsserver, struct netif *netif)
367368
{
368-
if (numdns < DNS_MAX_SERVERS) {
369-
if (dnsserver != NULL) {
370-
dns_servers[numdns] = (*dnsserver);
369+
370+
if (netif == NULL || netif_check_default(netif)) {
371+
if (numdns < DNS_MAX_SERVERS) {
372+
if (dnsserver != NULL) {
373+
dns_servers[numdns] = (*dnsserver);
374+
} else {
375+
dns_servers[numdns] = *IP_ADDR_ANY;
376+
}
377+
}
378+
} else {
379+
char name[INTERFACE_NAME_MAX_SIZE];
380+
sprintf(name, "%c%c%d", netif->name[0], netif->name[1], netif->num);
381+
dns_add_interface_server(numdns, name, dnsserver);
382+
}
383+
}
384+
385+
/**
386+
* @ingroup dns
387+
* Obtain one of the currently configured DNS server.
388+
*
389+
* @param numdns the index of the DNS server
390+
* @return IP address of the indexed DNS server or "ip_addr_any" if the DNS
391+
* server has not been configured.
392+
*/
393+
const ip_addr_t*
394+
dns_getserver(u8_t numdns, const char *interface_name)
395+
{
396+
if (interface_name == NULL) {
397+
if (numdns < DNS_MAX_SERVERS) {
398+
return &dns_servers[numdns];
371399
} else {
372-
dns_servers[numdns] = *IP_ADDR_ANY;
400+
return IP_ADDR_ANY;
401+
}
402+
} else {
403+
return dns_get_interface_server(numdns, interface_name);
404+
}
405+
}
406+
407+
/**
408+
* @ingroup dns
409+
* Initialize one of the DNS servers.
410+
*
411+
* @param numdns the index of the DNS server to set must be < DNS_MAX_SERVERS
412+
* @param dnsserver IP address of the DNS server to set
413+
*/
414+
void
415+
dns_add_interface_server(u8_t numdns, const char *interface_name, const ip_addr_t *dnsserver)
416+
{
417+
struct dns_server_interface *new_interface_server;
418+
419+
if (numdns >= DNS_MAX_SERVERS) {
420+
return;
421+
}
422+
423+
if (multihoming_dns_servers != NULL) {
424+
// if interface server already exists on the list just update it
425+
for (new_interface_server = multihoming_dns_servers; new_interface_server != NULL; new_interface_server = new_interface_server->next) {
426+
if (!strcmp(interface_name, new_interface_server->interface_name)) {
427+
new_interface_server->dns_servers[numdns] = (*dnsserver);
428+
return;
429+
}
373430
}
374431
}
432+
// add new dns server to the list tail
433+
new_interface_server = mem_malloc(sizeof(struct dns_server_interface));
434+
strncpy(new_interface_server->interface_name, interface_name, INTERFACE_NAME_MAX_SIZE);
435+
new_interface_server->dns_servers[numdns] = (*dnsserver);
436+
new_interface_server->next = NULL;
437+
438+
if (multihoming_dns_servers == NULL) {
439+
multihoming_dns_servers = new_interface_server;
440+
} else {
441+
struct dns_server_interface *tail;
442+
tail = multihoming_dns_servers;
443+
444+
while (tail->next != NULL) {
445+
tail = tail->next;
446+
}
447+
tail->next = new_interface_server;
448+
449+
}
450+
}
451+
452+
void
453+
dns_remove_interface_servers(const char *interface_name)
454+
{
455+
struct dns_server_interface *temp = multihoming_dns_servers;
456+
struct dns_server_interface *prev = NULL;
457+
458+
if (temp != NULL && !strcmp(interface_name, temp->interface_name)) {
459+
multihoming_dns_servers = temp->next;
460+
mem_free(temp);
461+
return;
462+
}
463+
464+
while (temp != NULL && strcmp(interface_name, temp->interface_name)) {
465+
prev = temp;
466+
temp = temp->next;
467+
}
468+
469+
if (temp == NULL) {
470+
return;
471+
}
472+
473+
prev->next = temp->next;
474+
mem_free(temp);
375475
}
376476

377477
/**
@@ -383,13 +483,20 @@ dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
383483
* server has not been configured.
384484
*/
385485
const ip_addr_t *
386-
dns_getserver(u8_t numdns)
486+
dns_get_interface_server(u8_t numdns, const char *interface_name)
387487
{
488+
struct dns_server_interface *interface_server;
489+
388490
if (numdns < DNS_MAX_SERVERS) {
389-
return &dns_servers[numdns];
390-
} else {
391491
return IP_ADDR_ANY;
392492
}
493+
494+
for (interface_server = multihoming_dns_servers; interface_server != NULL; interface_server = interface_server->next) {
495+
if (!strcmp(interface_name, interface_server->interface_name)) {
496+
return &interface_server->dns_servers[numdns];
497+
}
498+
}
499+
return IP_ADDR_ANY;
393500
}
394501

395502
/**

features/lwipstack/lwip/src/core/lwip_netif.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ struct netif *netif_default;
113113

114114
#define netif_index_to_num(index) ((index) - 1)
115115
static u8_t netif_num;
116+
static char netif_name [INTERFACE_NAME_MAX_SIZE];
116117

117118
#if LWIP_NUM_NETIF_CLIENT_DATA > 0
118119
static u8_t netif_client_id;
@@ -835,6 +836,26 @@ netif_set_default(struct netif *netif)
835836
netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
836837
}
837838

839+
/**
840+
* @ingroup netif
841+
* Checks if network interface is the default network interface
842+
* @param netif the default network interface
843+
* @return true if netif is set to default one
844+
* otherwise return false
845+
*/
846+
bool
847+
netif_check_default(struct netif *netif)
848+
{
849+
if (netif == NULL) {
850+
return false;
851+
}
852+
if (netif_default == netif) {
853+
return true;
854+
} else {
855+
return false;
856+
}
857+
}
858+
838859
/**
839860
* @ingroup netif
840861
* Bring an interface up, available for processing
@@ -1722,6 +1743,23 @@ netif_find(const char *name)
17221743
LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
17231744
return NULL;
17241745
}
1746+
1747+
/**
1748+
* @ingroup netif
1749+
* Get a network interface name
1750+
* @param netif
1751+
* @return name the name of the netif (like netif->name) plus concatenated number
1752+
* in ascii representation (e.g. 'en0')
1753+
*/
1754+
const char *
1755+
netif_get_name(struct netif *netif)
1756+
{
1757+
if (netif == NULL) {
1758+
return NULL;
1759+
}
1760+
sprintf(netif_name, "%c%c%d", netif->name[0], netif->name[1], netif->num);
1761+
return netif_name;
1762+
}
17251763

17261764
#if LWIP_NETIF_EXT_STATUS_CALLBACK
17271765
/**

features/lwipstack/lwip/src/core/lwip_tcp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ tcp_kill_prio(u8_t prio)
17231723
* We want to find the connections with the lowest possible prio, and among
17241724
* these the one with the longest inactivity time.
17251725
*/
1726-
//mprio--; temporary commented
1726+
mprio--;
17271727

17281728
inactivity = 0;
17291729
inactive = NULL;

features/lwipstack/lwip/src/core/lwip_tcp_in.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,12 @@ tcp_listen_input(struct tcp_pcb_listen *pcb)
703703
npcb->snd_wnd_max = npcb->snd_wnd;
704704

705705
#if TCP_CALCULATE_EFF_SEND_MSS
706-
npcb->mss = tcp_eff_send_mss(npcb->mss, &npcb->local_ip, &npcb->remote_ip);
706+
struct netif *netif;
707+
netif = netif_get_by_index(npcb->netif_idx);
708+
if(netif == NULL) {
709+
netif = ip_route(&npcb->local_ip, &npcb->remote_ip);
710+
}
711+
npcb->mss = tcp_eff_send_mss_netif(npcb->mss, netif, &npcb->remote_ip);
707712
#endif /* TCP_CALCULATE_EFF_SEND_MSS */
708713

709714
MIB2_STATS_INC(mib2.tcppassiveopens);
@@ -866,7 +871,12 @@ tcp_process(struct tcp_pcb *pcb)
866871
pcb->state = ESTABLISHED;
867872

868873
#if TCP_CALCULATE_EFF_SEND_MSS
869-
pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip);
874+
struct netif *netif;
875+
netif = netif_get_by_index(pcb->netif_idx);
876+
if(netif == NULL) {
877+
netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
878+
}
879+
pcb->mss = tcp_eff_send_mss_netif(pcb->mss, netif, &pcb->remote_ip);
870880
#endif /* TCP_CALCULATE_EFF_SEND_MSS */
871881

872882
pcb->cwnd = LWIP_TCP_CALC_INITIAL_CWND(pcb->mss);

features/lwipstack/lwip/src/include/lwip/dns.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
extern "C" {
5151
#endif
5252

53+
struct dns_server_interface {
54+
char interface_name [INTERFACE_NAME_MAX_SIZE];
55+
ip_addr_t dns_servers[DNS_MAX_SERVERS];
56+
struct dns_server_interface *next;
57+
};
58+
5359
/** DNS timer period */
5460
#define DNS_TMR_INTERVAL 1000
5561

@@ -104,8 +110,11 @@ typedef void (*dns_found_callback)(const char *name, const ip_addr_t *ipaddr, vo
104110

105111
void dns_init(void);
106112
void dns_tmr(void);
107-
void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver);
108-
const ip_addr_t* dns_getserver(u8_t numdns);
113+
void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver, struct netif *netif);
114+
const ip_addr_t* dns_getserver(u8_t numdns, const char *interface_name);
115+
void dns_add_interface_server(u8_t numdns, const char *interface_name, const ip_addr_t *dnsserver);
116+
void dns_remove_interface_servers(const char *interface_name);
117+
const ip_addr_t* dns_get_interface_server(u8_t numdns, const char *interface_name);
109118
err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr,
110119
dns_found_callback found, void *callback_arg);
111120
err_t dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr,

0 commit comments

Comments
 (0)