Skip to content

Commit 7d82eda

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 7d82eda

File tree

8 files changed

+71
-0
lines changed

8 files changed

+71
-0
lines changed

features/lwipstack/LWIPInterface.cpp

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

276+
char *LWIP::Interface::get_ipv6_link_local_address(char *buf, nsapi_size_t buflen)
277+
{
278+
const ip_addr_t *addr = LWIP::get_ipv6_link_local_addr(&netif);
279+
280+
if (!addr) {
281+
return NULL;
282+
}
283+
#if LWIP_IPV6
284+
if (IP_IS_V6(addr)) {
285+
return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen);
286+
}
287+
#endif
288+
return NULL;
289+
}
290+
276291
char *LWIP::Interface::get_ip_address(char *buf, nsapi_size_t buflen)
277292
{
278293
const ip_addr_t *addr = LWIP::get_ip_addr(true, &netif);

features/lwipstack/LWIPStack.h

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

103+
/** Copies IPv6 link local address of the network interface to user supplied buffer
104+
*
105+
* @param buf buffer to which IP address will be copied as "W:X:Y:Z"
106+
* @param buflen size of supplied buffer
107+
* @return Pointer to a buffer, or NULL if the buffer is too small
108+
*/
109+
char *get_ipv6_link_local_address(char *buf, nsapi_size_t buflen);
110+
103111
/** Copies IP address of the name based network interface to user supplied buffer
104112
*
105113
* @param buf buffer to which IP address will be copied as "W:X:Y:Z"
@@ -548,6 +556,7 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
548556
static const ip_addr_t *get_ip_addr(bool any_addr, const struct netif *netif);
549557
static const ip_addr_t *get_ipv4_addr(const struct netif *netif);
550558
static const ip_addr_t *get_ipv6_addr(const struct netif *netif);
559+
static const ip_addr_t *get_ipv6_link_local_addr(const struct netif *netif);
551560

552561
static void add_dns_addr(struct netif *lwip_netif, const char *interface_name);
553562

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+
const char *EMACInterface::get_ipv6_link_local_address()
99+
{
100+
if (_interface && _interface->get_ipv6_link_local_address(_ip_address, sizeof(_ip_address))) {
101+
return _ip_address;
102+
}
103+
104+
return NULL;
105+
}
106+
98107
const char *EMACInterface::get_netmask()
99108
{
100109
if (_interface && _interface->get_netmask(_netmask, sizeof(_netmask))) {

features/netsocket/EMACInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class EMACInterface : public virtual NetworkInterface {
102102
* or null if no IP address has been received
103103
*/
104104
virtual const char *get_ip_address();
105+
virtual const char *get_ipv6_link_local_address();
105106

106107
/** Get the local network mask
107108
*

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+
const char *NetworkInterface::get_ipv6_link_local_address()
41+
{
42+
return 0;
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 const char *get_ipv6_link_local_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 char *get_ipv6_link_local_address(char *buf, nsapi_size_t buflen)
130+
{
131+
return NULL;
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)