Skip to content

Commit fe8cd5d

Browse files
author
Jayasankar Nara
committed
Add API to get ipv6 link local address.
Protocols like mdns requires IPv6 link local address to be advertised in its records (AAAA record). LWIP::Interface::bringup() API is creating IPv6 link local address;But as of now there is no API exposed by mbed-os to get the IPv6 link local address. This new API is required to deliver mDNS library support on mbed-os for Cypress platforms. Unit tested it by invoking get_ipv6_link_local_address with a simple application.
1 parent 3c9853a commit fe8cd5d

File tree

16 files changed

+127
-4
lines changed

16 files changed

+127
-4
lines changed

TESTS/network/interface/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,14 @@ Test `NetworkInterface::get_connection_status()`.
155155
1. Check that `get_connection_status()` returns status `NSAPI_STATUS_DISCONNECTED`.
156156
2. Connect interface.
157157
3. Poll the `get_connection_status()` until it returns status `NSAPI_STATUS_GLOBAL_UP`.
158-
4. Disconnect interface.
159-
5. Check that `get_connection_status()` returns status `NSAPI_STATUS_DISCONNECTED`.
160-
6. Repeat connect and disconnect steps 2 to 5 four times.
158+
4. (IPv6 only) Get IPv6 link local address using `get_ipv6_link_local_address` API.
159+
5. (IPv6 only) Check that `get_ipv6_link_local_address` returned status `NSAPI_ERROR_OK`.
160+
6. (IPv6 only) Check that the IP address associated with the Socket Address is not `NULL`.
161+
7. (IPv6 only) Check that the IP version of the IPv6 link local address is `NSAPI_IPv6`.
162+
8. Disconnect interface.
163+
9. Check that `get_connection_status()` returns status `NSAPI_STATUS_DISCONNECTED`.
164+
10. Repeat connect and disconnect steps 2 to 5 four times.
161165

162166
**Expected result:**
163167

164-
`Connect()` and `disconnect()` calls return `NSAPI_ERROR_OK`. The right status is returned by `get_connection_status()`.
168+
`Connect()`, `get_ipv6_link_local_address` and `disconnect()` calls return `NSAPI_ERROR_OK`. The right status is returned by `get_connection_status()`. And the right IPv6 link local address is returned by `get_ipv6_link_local_address`.

TESTS/network/interface/networkinterface_status.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ void NETWORKINTERFACE_STATUS_GET()
156156
ThisThread::sleep_for(500);
157157
}
158158

159+
#if MBED_CONF_LWIP_IPV6_ENABLED
160+
/* if IPv6 is enabled, validate ipv6_link_local_address API*/
161+
SocketAddress ipv6_link_local_address = NULL;
162+
err = net->get_ipv6_link_local_address(&ipv6_link_local_address);
163+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
164+
TEST_ASSERT_NOT_NULL(ipv6_link_local_address.get_ip_address());
165+
TEST_ASSERT_EQUAL(NSAPI_IPv6, ipv6_link_local_address.get_ip_version());
166+
#endif
167+
159168
err = net->disconnect();
160169
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
161170

UNITTESTS/features/netsocket/EthernetInterface/test_EthernetInterface.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class EmacNetworkStackMock : public OnboardNetworkStack {
9494
MOCK_CONST_METHOD0(get_connection_status, nsapi_connection_status_t());
9595
MOCK_METHOD2(get_mac_address, char *(char *buf, nsapi_size_t buflen));
9696
MOCK_METHOD2(get_ip_address, char *(char *buf, nsapi_size_t buflen));
97+
MOCK_METHOD1(get_ipv6_link_local_address, nsapi_error_t(SocketAddress *address));
9798
MOCK_METHOD2(get_netmask, char *(char *buf, nsapi_size_t buflen));
9899
MOCK_METHOD2(get_gateway, char *(char *buf, nsapi_size_t buflen));
99100
};

UNITTESTS/stubs/NetworkInterface_stub.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ const char *NetworkInterface::get_ip_address()
3131
return 0;
3232
}
3333

34+
nsapi_error_t NetworkInterface::get_ipv6_link_local_address(SocketAddress *address)
35+
{
36+
return NSAPI_ERROR_UNSUPPORTED;
37+
}
38+
3439
const char *NetworkInterface::get_netmask()
3540
{
3641
return 0;

UNITTESTS/stubs/NetworkStack_stub.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ const char *NetworkStack::get_ip_address()
9393
{
9494
return NULL;
9595
}
96+
97+
nsapi_error_t NetworkStack::get_ipv6_link_local_address(SocketAddress *address)
98+
{
99+
return NSAPI_ERROR_UNSUPPORTED;
100+
}
101+
96102
const char *NetworkStack::get_ip_address_if(const char *interface_name)
97103
{
98104
return NULL;

features/lwipstack/LWIPInterface.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "lwip/udp.h"
3838

3939
#include "LWIPStack.h"
40+
#include "lwip_tools.h"
4041

4142
LWIP::Interface *LWIP::Interface::list;
4243

@@ -271,6 +272,30 @@ char *LWIP::Interface::get_interface_name(char *buf)
271272
return buf;
272273
}
273274

275+
nsapi_error_t LWIP::Interface::get_ipv6_link_local_address(SocketAddress *address)
276+
{
277+
#if LWIP_IPV6
278+
const ip_addr_t *addr = LWIP::get_ipv6_link_local_addr(&netif);
279+
nsapi_addr_t out;
280+
bool ret;
281+
282+
if (!addr) {
283+
return NSAPI_ERROR_PARAMETER;
284+
}
285+
286+
ret = convert_lwip_addr_to_mbed(&out, addr);
287+
if (ret != true) {
288+
return NSAPI_ERROR_PARAMETER;
289+
}
290+
291+
address->set_addr(out);
292+
293+
return NSAPI_ERROR_OK;
294+
#else
295+
return NSAPI_ERROR_UNSUPPORTED;
296+
#endif
297+
}
298+
274299
char *LWIP::Interface::get_ip_address(char *buf, nsapi_size_t buflen)
275300
{
276301
const ip_addr_t *addr = LWIP::get_ip_addr(true, &netif);

features/lwipstack/LWIPStack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
101101
*/
102102
virtual char *get_ip_address(char *buf, nsapi_size_t buflen);
103103

104+
/** Get the IPv6 link local address in SocketAddress representation
105+
*
106+
* @address SocketAddress representation of the link local IPv6 address
107+
* @return NSAPI_ERROR_OK on success, or error code
108+
*/
109+
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address);
110+
104111
/** Copies IP address of the name based network interface to user supplied buffer
105112
*
106113
* @param buf buffer to which IP address will be copied as "W:X:Y:Z"
@@ -571,6 +578,7 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
571578
static const ip_addr_t *get_ip_addr(bool any_addr, const struct netif *netif);
572579
static const ip_addr_t *get_ipv4_addr(const struct netif *netif);
573580
static const ip_addr_t *get_ipv6_addr(const struct netif *netif);
581+
static const ip_addr_t *get_ipv6_link_local_addr(const struct netif *netif);
574582

575583
static void add_dns_addr(struct netif *lwip_netif, const char *interface_name);
576584

features/lwipstack/lwip_tools.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ const ip_addr_t *LWIP::get_ipv4_addr(const struct netif *netif)
7979
return NULL;
8080
}
8181

82+
const ip_addr_t *LWIP::get_ipv6_link_local_addr(const struct netif *netif)
83+
{
84+
#if LWIP_IPV6
85+
if (!netif_is_up(netif)) {
86+
return NULL;
87+
}
88+
89+
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
90+
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
91+
ip6_addr_islinklocal(netif_ip6_addr(netif, i))) {
92+
return netif_ip_addr6(netif, i);
93+
}
94+
}
95+
#endif
96+
return NULL;
97+
}
98+
8299
const ip_addr_t *LWIP::get_ipv6_addr(const struct netif *netif)
83100
{
84101
#if LWIP_IPV6

features/nanostack/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class Nanostack::Interface : public OnboardNetworkStack::Interface, private mbed::NonCopyable<Nanostack::Interface> {
2727
public:
2828
virtual char *get_ip_address(char *buf, nsapi_size_t buflen);
29+
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address);
2930
virtual char *get_mac_address(char *buf, nsapi_size_t buflen);
3031
virtual char *get_netmask(char *buf, nsapi_size_t buflen);
3132
virtual char *get_gateway(char *buf, nsapi_size_t buflen);

features/netsocket/EMACInterface.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ const char *EMACInterface::get_ip_address()
9595
return NULL;
9696
}
9797

98+
nsapi_error_t EMACInterface::get_ipv6_link_local_address(SocketAddress *address)
99+
{
100+
if (_interface) {
101+
return _interface->get_ipv6_link_local_address(address);
102+
}
103+
104+
return NSAPI_ERROR_NO_CONNECTION;
105+
}
106+
98107
const char *EMACInterface::get_netmask()
99108
{
100109
if (_interface && _interface->get_netmask(_netmask, sizeof(_netmask))) {

features/netsocket/EMACInterface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ class EMACInterface : public virtual NetworkInterface {
103103
*/
104104
virtual const char *get_ip_address();
105105

106+
/** Get the IPv6 link local address
107+
*
108+
* @address SocketAddress representation of the link local IPv6 address
109+
* @return 0 on success, negative error code on failure
110+
*/
111+
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address);
112+
106113
/** Get the local network mask
107114
*
108115
* @return Null-terminated representation of the local network mask

features/netsocket/NetworkInterface.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const char *NetworkInterface::get_ip_address()
3737
return 0;
3838
}
3939

40+
nsapi_error_t NetworkInterface::get_ipv6_link_local_address(SocketAddress *address)
41+
{
42+
return NSAPI_ERROR_UNSUPPORTED;
43+
}
44+
4045
const char *NetworkInterface::get_netmask()
4146
{
4247
return 0;

features/netsocket/NetworkInterface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ class NetworkInterface: public DNS {
107107
*/
108108
virtual const char *get_ip_address();
109109

110+
/** Get the IPv6 link local address
111+
*
112+
* @address SocketAddress representation of the link local IPv6 address
113+
* @return NSAPI_ERROR_OK on success, negative error code on failure
114+
*/
115+
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address);
116+
110117
/** Get the local network mask.
111118
*
112119
* @return Null-terminated representation of the local network mask

features/netsocket/NetworkStack.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ const char *NetworkStack::get_ip_address()
2929
return 0;
3030
}
3131

32+
nsapi_error_t NetworkStack::get_ipv6_link_local_address(SocketAddress *address)
33+
{
34+
return NSAPI_ERROR_UNSUPPORTED;
35+
}
36+
3237
const char *NetworkStack::get_ip_address_if(const char *interface_name)
3338
{
3439
return 0;

features/netsocket/NetworkStack.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class NetworkStack: public DNS {
4848
*/
4949
virtual const char *get_ip_address();
5050

51+
/** Get the IPv6 link local address
52+
*
53+
* @address SocketAddress representation of the link local IPv6 address
54+
* @return NSAPI_ERROR_OK on success, negative error code on failure
55+
*/
56+
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address);
57+
5158
/** Get the local IP address on interface name
5259
*
5360
* @param interface_name Network interface_name

features/netsocket/OnboardNetworkStack.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ class OnboardNetworkStack : public NetworkStack {
119119

120120
virtual char *get_ip_address(char *buf, nsapi_size_t buflen) = 0;
121121

122+
/** Copies IPv6 link local address of the network interface in SocketAddress format
123+
*
124+
* @address SocketAddress representation of the link local IPv6 address
125+
* @return NSAPI_ERROR_OK on success, negative error code on failure
126+
*/
127+
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address) = 0;
128+
122129
/** Copies IP address of the network interface to user supplied buffer
123130
*
124131
* @param buf buffer to which IP address will be copied as "W:X:Y:Z"

0 commit comments

Comments
 (0)