Skip to content

Commit b728142

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 ff4dac9 commit b728142

File tree

8 files changed

+107
-0
lines changed

8 files changed

+107
-0
lines changed

features/lwipstack/LWIPInterface.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,52 @@ char *LWIP::Interface::get_interface_name(char *buf)
273273
return buf;
274274
}
275275

276+
static bool convert_lwip_addr_to_mbed(nsapi_addr_t *out, const ip_addr_t *in)
277+
{
278+
#if LWIP_IPV6
279+
if (IP_IS_V6(in)) {
280+
out->version = NSAPI_IPv6;
281+
SMEMCPY(out->bytes, ip_2_ip6(in), sizeof(ip6_addr_t));
282+
return true;
283+
}
284+
#endif
285+
#if LWIP_IPV4
286+
if (IP_IS_V4(in)) {
287+
out->version = NSAPI_IPv4;
288+
SMEMCPY(out->bytes, ip_2_ip4(in), sizeof(ip4_addr_t));
289+
return true;
290+
}
291+
#endif
292+
#if LWIP_IPV6 && LWIP_IPV4
293+
return false;
294+
#endif
295+
}
296+
297+
nsapi_error_t LWIP::Interface::get_ipv6_link_local_address(SocketAddress *address)
298+
{
299+
#if LWIP_IPV6
300+
const ip_addr_t *addr = LWIP::get_ipv6_link_local_addr(&netif);
301+
nsapi_addr_t out;
302+
bool ret;
303+
304+
if (!addr) {
305+
return NSAPI_ERROR_PARAMETER;
306+
}
307+
308+
ret = convert_lwip_addr_to_mbed(&out, addr);
309+
if(ret != true)
310+
{
311+
return NSAPI_ERROR_PARAMETER;
312+
}
313+
314+
address->set_addr(out);
315+
316+
return NSAPI_ERROR_OK;
317+
#else
318+
return NSAPI_ERROR_PARAMETER;
319+
#endif
320+
}
321+
276322
char *LWIP::Interface::get_ip_address(char *buf, nsapi_size_t buflen)
277323
{
278324
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
@@ -100,6 +100,13 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
100100
*/
101101
virtual char *get_ip_address(char *buf, nsapi_size_t buflen);
102102

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

552560
static void add_dns_addr(struct netif *lwip_netif, const char *interface_name);
553561

features/lwipstack/lwip_tools.cpp

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

77+
const ip_addr_t *LWIP::get_ipv6_link_local_addr(const struct netif *netif)
78+
{
79+
#if LWIP_IPV6
80+
if (!netif_is_up(netif)) {
81+
return NULL;
82+
}
83+
84+
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
85+
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
86+
ip6_addr_islinklocal(netif_ip6_addr(netif, i))) {
87+
return netif_ip_addr6(netif, i);
88+
}
89+
}
90+
#endif
91+
return NULL;
92+
}
93+
7794
const ip_addr_t *LWIP::get_ipv6_addr(const struct netif *netif)
7895
{
7996
#if LWIP_IPV6

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 && _interface->get_ipv6_link_local_address(address)) {
101+
return NSAPI_ERROR_PARAMETER;
102+
}
103+
104+
return NSAPI_ERROR_OK;
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class NetworkInterface: public DNS {
106106
*/
107107
virtual const char *get_ip_address();
108108

109+
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address);
110+
109111
/** Get the local network mask.
110112
*
111113
* @return Null-terminated representation of the local network mask

features/netsocket/OnboardNetworkStack.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ class OnboardNetworkStack : public NetworkStack {
118118

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

121+
/** Copies IPv6 link local address of the network interface to user supplied buffer
122+
*
123+
* @param buf buffer to which IP address will be copied as "W:X:Y:Z"
124+
* @param buflen size of supplied buffer
125+
* @param interface_name Network interface name
126+
* @return Pointer to a buffer, or NULL if the buffer is too small
127+
*/
128+
129+
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address)
130+
{
131+
return NSAPI_ERROR_OK;
132+
}
133+
121134
/** Copies IP address of the network interface to user supplied buffer
122135
*
123136
* @param buf buffer to which IP address will be copied as "W:X:Y:Z"

0 commit comments

Comments
 (0)