Skip to content

Commit 5bef46b

Browse files
authored
Merge pull request #12063 from michalpasztamobica/get_ip_address_if_fixed
Move get_ip_address_if() to NetworkStack and include it in multihoming test
2 parents 42fc0e0 + c2856ef commit 5bef46b

File tree

5 files changed

+48
-81
lines changed

5 files changed

+48
-81
lines changed

TESTS/network/multihoming/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "utest.h"
3838
#include "utest/utest_stack_trace.h"
3939
#include "multihoming_tests.h"
40+
#include "LWIPStack.h"
4041

4142
using namespace utest::v1;
4243

@@ -77,6 +78,10 @@ static void _ifup()
7778
SocketAddress eth_ip_address;
7879
eth->get_ip_address(&eth_ip_address);
7980
printf("MBED: IP address is '%s' interface name %s\n", eth_ip_address.get_ip_address(), interface_name[interface_num]);
81+
SocketAddress eth_ip_address_if;
82+
LWIP::get_instance().get_ip_address_if(&eth_ip_address_if, interface_name[interface_num]);
83+
printf("IP_if: %s\n", eth_ip_address.get_ip_address());
84+
TEST_ASSERT_EQUAL(eth_ip_address_if, eth_ip_address);
8085
interface_num++;
8186

8287
wifi = WiFiInterface::get_default_instance();
@@ -104,6 +109,10 @@ static void _ifup()
104109
SocketAddress wifi_ip_address;
105110
wifi->get_ip_address(&wifi_ip_address);
106111
printf("IP: %s\n", STRING_VERIFY(wifi_ip_address.get_ip_address()));
112+
SocketAddress wifi_ip_address_if;
113+
LWIP::get_instance().get_ip_address_if(&wifi_ip_address_if, interface_name[interface_num]);
114+
printf("IP_if: %s\n", STRING_VERIFY(wifi_ip_address_if.get_ip_address()));
115+
TEST_ASSERT_EQUAL(wifi_ip_address_if, wifi_ip_address);
107116
SocketAddress wifi_netmask;
108117
wifi->get_netmask(&wifi_netmask);
109118
printf("Netmask: %s\n", STRING_VERIFY(wifi_netmask.get_ip_address()));

features/lwipstack/LWIPInterface.cpp

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -349,63 +349,6 @@ char *LWIP::Interface::get_ip_address(char *buf, nsapi_size_t buflen)
349349
#endif
350350
}
351351

352-
nsapi_error_t LWIP::Interface::get_ip_address_if(const char *interface_name, SocketAddress *address)
353-
{
354-
if (!address) {
355-
return NSAPI_ERROR_PARAMETER;
356-
}
357-
358-
const ip_addr_t *addr;
359-
360-
if (interface_name == NULL) {
361-
addr = LWIP::get_ip_addr(true, &netif);
362-
} else {
363-
addr = LWIP::get_ip_addr(true, netif_find(interface_name));
364-
}
365-
#if LWIP_IPV6
366-
if (IP_IS_V6(addr)) {
367-
char buf[NSAPI_IPv6_SIZE];
368-
address->set_ip_address(ip6addr_ntoa_r(ip_2_ip6(addr), buf, NSAPI_IPv6_SIZE));
369-
return NSAPI_ERROR_OK;
370-
}
371-
#endif
372-
#if LWIP_IPV4
373-
if (IP_IS_V4(addr)) {
374-
char buf[NSAPI_IPv4_SIZE];
375-
address->set_ip_address(ip4addr_ntoa_r(ip_2_ip4(addr), buf, NSAPI_IPv4_SIZE));
376-
return NSAPI_ERROR_OK;
377-
}
378-
#endif
379-
return NSAPI_ERROR_UNSUPPORTED;
380-
}
381-
382-
char *LWIP::Interface::get_ip_address_if(char *buf, nsapi_size_t buflen, const char *interface_name)
383-
{
384-
const ip_addr_t *addr;
385-
386-
if (interface_name == NULL) {
387-
addr = LWIP::get_ip_addr(true, &netif);
388-
} else {
389-
addr = LWIP::get_ip_addr(true, netif_find(interface_name));
390-
}
391-
if (!addr) {
392-
return NULL;
393-
}
394-
#if LWIP_IPV6
395-
if (IP_IS_V6(addr)) {
396-
return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen);
397-
}
398-
#endif
399-
#if LWIP_IPV4
400-
if (IP_IS_V4(addr)) {
401-
return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen);
402-
}
403-
#endif
404-
#if LWIP_IPV6 && LWIP_IPV4
405-
return NULL;
406-
#endif
407-
}
408-
409352
nsapi_error_t LWIP::Interface::get_netmask(SocketAddress *address)
410353
{
411354
if (!address) {

features/lwipstack/LWIPStack.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,36 @@ const char *LWIP::get_ip_address()
217217
#endif
218218
}
219219

220+
nsapi_error_t LWIP::get_ip_address_if(SocketAddress *address, const char *interface_name)
221+
{
222+
if (!address) {
223+
return NSAPI_ERROR_PARAMETER;
224+
}
225+
226+
const ip_addr_t *addr;
227+
228+
if (interface_name == NULL) {
229+
addr = get_ip_addr(true, &default_interface->netif);
230+
} else {
231+
addr = get_ip_addr(true, netif_find(interface_name));
232+
}
233+
#if LWIP_IPV6
234+
if (IP_IS_V6(addr)) {
235+
char buf[NSAPI_IPv6_SIZE];
236+
address->set_ip_address(ip6addr_ntoa_r(ip_2_ip6(addr), buf, NSAPI_IPv6_SIZE));
237+
return NSAPI_ERROR_OK;
238+
}
239+
#endif
240+
#if LWIP_IPV4
241+
if (IP_IS_V4(addr)) {
242+
char buf[NSAPI_IPv4_SIZE];
243+
address->set_ip_address(ip4addr_ntoa_r(ip_2_ip4(addr), buf, NSAPI_IPv4_SIZE));
244+
return NSAPI_ERROR_OK;
245+
}
246+
#endif
247+
return NSAPI_ERROR_UNSUPPORTED;
248+
}
249+
220250
nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)
221251
{
222252
// check if network is connected

features/lwipstack/LWIPStack.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,6 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
106106
*/
107107
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address);
108108

109-
/** Copies IP address of the name based network interface to user supplied buffer
110-
*
111-
* @param buf buffer to which IP address will be copied as "W:X:Y:Z"
112-
* @param buflen size of supplied buffer
113-
* @param interface_name naame of the interface
114-
* @return Pointer to a buffer, or NULL if the buffer is too small
115-
*/
116-
virtual nsapi_error_t get_ip_address_if(const char *interface_name, SocketAddress *address);
117-
118-
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
119-
virtual char *get_ip_address_if(char *buf, nsapi_size_t buflen, const char *interface_name);
120-
121109
/** Copies netmask of the network interface to user supplied buffer
122110
*
123111
* @param buf buffer to which netmask will be copied as "W:X:Y:Z"
@@ -323,6 +311,15 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
323311
* or null if not yet connected
324312
*/
325313
virtual const char *get_ip_address();
314+
315+
/** Copies IP address of the name based network interface to user supplied buffer
316+
*
317+
* @param address SocketAddress object pointer to store the address
318+
* @param interface_name name of the interface
319+
* @return Pointer to a buffer, or NULL if the buffer is too small
320+
*/
321+
virtual nsapi_error_t get_ip_address_if(SocketAddress *address, const char *interface_name);
322+
326323
/** Set the network interface as default one
327324
*/
328325
virtual void set_default_interface(OnboardNetworkStack::Interface *interface);

features/netsocket/OnboardNetworkStack.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,6 @@ class OnboardNetworkStack : public NetworkStack {
121121
return NSAPI_ERROR_UNSUPPORTED;
122122
}
123123

124-
/** @copydoc NetworkStack::get_ip_address_if */
125-
virtual nsapi_error_t get_ip_address_if(SocketAddress *address, const char *interface_name)
126-
{
127-
return NSAPI_ERROR_UNSUPPORTED;
128-
}
129-
130-
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
131-
virtual char *get_ip_address_if(char *buf, nsapi_size_t buflen, const char *interface_name)
132-
{
133-
return NULL;
134-
};
135-
136124
/** @copydoc NetworkStack::get_netmask */
137125
virtual nsapi_error_t get_netmask(SocketAddress *address) = 0;
138126

0 commit comments

Comments
 (0)