Skip to content

Move get_ip_address_if() to NetworkStack and include it in multihoming test #12063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions TESTS/network/multihoming/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "utest.h"
#include "utest/utest_stack_trace.h"
#include "multihoming_tests.h"
#include "LWIPStack.h"

using namespace utest::v1;

Expand Down Expand Up @@ -77,6 +78,10 @@ static void _ifup()
SocketAddress eth_ip_address;
eth->get_ip_address(&eth_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(&eth_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();
Expand Down Expand Up @@ -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()));
Expand Down
57 changes: 0 additions & 57 deletions features/lwipstack/LWIPInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions features/lwipstack/LWIPStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 9 additions & 12 deletions features/lwipstack/LWIPStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,6 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
*/
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"
Expand Down Expand Up @@ -323,6 +311,15 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
* 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);
Expand Down
12 changes: 0 additions & 12 deletions features/netsocket/OnboardNetworkStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,6 @@ class OnboardNetworkStack : public NetworkStack {
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;

Expand Down