diff --git a/TESTS/network/multihoming/main.cpp b/TESTS/network/multihoming/main.cpp index 79c898d525..ed8f087e62 100644 --- a/TESTS/network/multihoming/main.cpp +++ b/TESTS/network/multihoming/main.cpp @@ -37,6 +37,7 @@ #include "utest.h" #include "utest/utest_stack_trace.h" #include "multihoming_tests.h" +#include "LWIPStack.h" using namespace utest::v1; @@ -77,6 +78,10 @@ static void _ifup() SocketAddress eth_ip_address; eth->get_ip_address(ð_ip_address); printf("MBED: IP address is '%s' interface name %s\n", eth_ip_address.get_ip_address(), interface_name[interface_num]); + SocketAddress eth_ip_address_if; + LWIP::get_instance().get_ip_address_if(ð_ip_address_if, interface_name[interface_num]); + printf("IP_if: %s\n", eth_ip_address.get_ip_address()); + TEST_ASSERT_EQUAL(eth_ip_address_if, eth_ip_address); interface_num++; wifi = WiFiInterface::get_default_instance(); @@ -104,6 +109,10 @@ static void _ifup() SocketAddress wifi_ip_address; wifi->get_ip_address(&wifi_ip_address); printf("IP: %s\n", STRING_VERIFY(wifi_ip_address.get_ip_address())); + SocketAddress wifi_ip_address_if; + LWIP::get_instance().get_ip_address_if(&wifi_ip_address_if, interface_name[interface_num]); + printf("IP_if: %s\n", STRING_VERIFY(wifi_ip_address_if.get_ip_address())); + TEST_ASSERT_EQUAL(wifi_ip_address_if, wifi_ip_address); SocketAddress wifi_netmask; wifi->get_netmask(&wifi_netmask); printf("Netmask: %s\n", STRING_VERIFY(wifi_netmask.get_ip_address())); diff --git a/features/lwipstack/LWIPInterface.cpp b/features/lwipstack/LWIPInterface.cpp index 1fe8f4f92e..e6e59a450a 100644 --- a/features/lwipstack/LWIPInterface.cpp +++ b/features/lwipstack/LWIPInterface.cpp @@ -349,63 +349,6 @@ char *LWIP::Interface::get_ip_address(char *buf, nsapi_size_t buflen) #endif } -nsapi_error_t LWIP::Interface::get_ip_address_if(const char *interface_name, SocketAddress *address) -{ - if (!address) { - return NSAPI_ERROR_PARAMETER; - } - - const ip_addr_t *addr; - - if (interface_name == NULL) { - addr = LWIP::get_ip_addr(true, &netif); - } else { - addr = LWIP::get_ip_addr(true, netif_find(interface_name)); - } -#if LWIP_IPV6 - if (IP_IS_V6(addr)) { - char buf[NSAPI_IPv6_SIZE]; - address->set_ip_address(ip6addr_ntoa_r(ip_2_ip6(addr), buf, NSAPI_IPv6_SIZE)); - return NSAPI_ERROR_OK; - } -#endif -#if LWIP_IPV4 - if (IP_IS_V4(addr)) { - char buf[NSAPI_IPv4_SIZE]; - address->set_ip_address(ip4addr_ntoa_r(ip_2_ip4(addr), buf, NSAPI_IPv4_SIZE)); - return NSAPI_ERROR_OK; - } -#endif - return NSAPI_ERROR_UNSUPPORTED; -} - -char *LWIP::Interface::get_ip_address_if(char *buf, nsapi_size_t buflen, const char *interface_name) -{ - const ip_addr_t *addr; - - if (interface_name == NULL) { - addr = LWIP::get_ip_addr(true, &netif); - } else { - addr = LWIP::get_ip_addr(true, netif_find(interface_name)); - } - if (!addr) { - return NULL; - } -#if LWIP_IPV6 - if (IP_IS_V6(addr)) { - return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen); - } -#endif -#if LWIP_IPV4 - if (IP_IS_V4(addr)) { - return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen); - } -#endif -#if LWIP_IPV6 && LWIP_IPV4 - return NULL; -#endif -} - nsapi_error_t LWIP::Interface::get_netmask(SocketAddress *address) { if (!address) { diff --git a/features/lwipstack/LWIPStack.cpp b/features/lwipstack/LWIPStack.cpp index 254863e17a..01d9f3ca0f 100644 --- a/features/lwipstack/LWIPStack.cpp +++ b/features/lwipstack/LWIPStack.cpp @@ -217,6 +217,36 @@ const char *LWIP::get_ip_address() #endif } +nsapi_error_t LWIP::get_ip_address_if(SocketAddress *address, const char *interface_name) +{ + if (!address) { + return NSAPI_ERROR_PARAMETER; + } + + const ip_addr_t *addr; + + if (interface_name == NULL) { + addr = get_ip_addr(true, &default_interface->netif); + } else { + addr = get_ip_addr(true, netif_find(interface_name)); + } +#if LWIP_IPV6 + if (IP_IS_V6(addr)) { + char buf[NSAPI_IPv6_SIZE]; + address->set_ip_address(ip6addr_ntoa_r(ip_2_ip6(addr), buf, NSAPI_IPv6_SIZE)); + return NSAPI_ERROR_OK; + } +#endif +#if LWIP_IPV4 + if (IP_IS_V4(addr)) { + char buf[NSAPI_IPv4_SIZE]; + address->set_ip_address(ip4addr_ntoa_r(ip_2_ip4(addr), buf, NSAPI_IPv4_SIZE)); + return NSAPI_ERROR_OK; + } +#endif + return NSAPI_ERROR_UNSUPPORTED; +} + nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) { // check if network is connected diff --git a/features/lwipstack/LWIPStack.h b/features/lwipstack/LWIPStack.h index 18cf9d54f6..3d25b304fb 100644 --- a/features/lwipstack/LWIPStack.h +++ b/features/lwipstack/LWIPStack.h @@ -106,18 +106,6 @@ public: */ virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address); - /** Copies IP address of the name based network interface to user supplied buffer - * - * @param buf buffer to which IP address will be copied as "W:X:Y:Z" - * @param buflen size of supplied buffer - * @param interface_name naame of the interface - * @return Pointer to a buffer, or NULL if the buffer is too small - */ - virtual nsapi_error_t get_ip_address_if(const char *interface_name, SocketAddress *address); - - MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated") - virtual char *get_ip_address_if(char *buf, nsapi_size_t buflen, const char *interface_name); - /** Copies netmask of the network interface to user supplied buffer * * @param buf buffer to which netmask will be copied as "W:X:Y:Z" @@ -323,6 +311,15 @@ public: * or null if not yet connected */ virtual const char *get_ip_address(); + + /** Copies IP address of the name based network interface to user supplied buffer + * + * @param address SocketAddress object pointer to store the address + * @param interface_name name of the interface + * @return Pointer to a buffer, or NULL if the buffer is too small + */ + virtual nsapi_error_t get_ip_address_if(SocketAddress *address, const char *interface_name); + /** Set the network interface as default one */ virtual void set_default_interface(OnboardNetworkStack::Interface *interface); diff --git a/features/netsocket/OnboardNetworkStack.h b/features/netsocket/OnboardNetworkStack.h index 7a3f25d9ea..2d7f308283 100644 --- a/features/netsocket/OnboardNetworkStack.h +++ b/features/netsocket/OnboardNetworkStack.h @@ -121,18 +121,6 @@ public: return NSAPI_ERROR_UNSUPPORTED; } - /** @copydoc NetworkStack::get_ip_address_if */ - virtual nsapi_error_t get_ip_address_if(SocketAddress *address, const char *interface_name) - { - return NSAPI_ERROR_UNSUPPORTED; - } - - MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated") - virtual char *get_ip_address_if(char *buf, nsapi_size_t buflen, const char *interface_name) - { - return NULL; - }; - /** @copydoc NetworkStack::get_netmask */ virtual nsapi_error_t get_netmask(SocketAddress *address) = 0;