Skip to content

Netsocket: Introduce set_ip_address and get_dns_server APIs #12606

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
Apr 15, 2020
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
5 changes: 5 additions & 0 deletions UNITTESTS/stubs/NetworkInterface_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ nsapi_error_t NetworkInterface::add_dns_server(const SocketAddress &address, con
return NSAPI_ERROR_UNSUPPORTED;
}

nsapi_error_t NetworkInterface::get_dns_server(int index, SocketAddress *address, const char *interface_name)
{
return NSAPI_ERROR_UNSUPPORTED;
}

void NetworkInterface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
{

Expand Down
72 changes: 57 additions & 15 deletions features/lwipstack/LWIPInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,23 +680,13 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
}
#endif /* LWIP_IPV6 */

#if LWIP_IPV4
if (stack != IPV6_STACK) {
if (!dhcp && !ppp_enabled) {
ip4_addr_t ip_addr;
ip4_addr_t netmask_addr;
ip4_addr_t gw_addr;

if (!inet_aton(ip, &ip_addr) ||
!inet_aton(netmask, &netmask_addr) ||
!inet_aton(gw, &gw_addr)) {
return NSAPI_ERROR_PARAMETER;
}

netif_set_addr(&netif, &ip_addr, &netmask_addr, &gw_addr);
nsapi_error_t ret_add_ip = NSAPI_ERROR_OK;
if (!dhcp && !ppp_enabled) {
ret_add_ip = set_ip_address(ip, netmask, gw);
if (ret_add_ip != NSAPI_ERROR_OK) {
return ret_add_ip;
}
}
#endif

if (client_callback) {
client_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_CONNECTING);
Expand Down Expand Up @@ -753,6 +743,58 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
return NSAPI_ERROR_OK;
}

nsapi_error_t LWIP::Interface::set_ip_address(const char *ip,
const char *netmask,
const char *gw,
uint8_t ipv6_flag)
{
if (!ip) {
return NSAPI_ERROR_PARAMETER;
}

int conv_ip = 1;

#if LWIP_IPV4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both LWIP_IPV4 & 6 are enabled, can you still set IPv6 address? inet_aton returns NULL in case of wrong format? Maybe a comment would be needed here to make it clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you can. In one of our projects we set up static IPv4 address using set_ip_address API and then give link-local IPv6 address in bringup. And bringup internally uses the same set_ip_address to set IPv6 address.

ip4_addr_t ip_addr;
ip4_addr_t netmask_addr;
ip4_addr_t gw_addr;
IP4_ADDR(&netmask_addr, 255, 255, 255, 255);
ip4_addr_set_zero(&gw_addr);
int conv_netmask = 1;
int conv_gw = 1;

conv_ip = inet_aton(ip, &ip_addr);
if (netmask) {
conv_netmask = inet_aton(netmask, &netmask_addr);
}
if (gw) {
conv_gw = inet_aton(gw, &gw_addr);
}
if (conv_ip && conv_netmask && conv_gw) {
netif_set_addr(&netif, &ip_addr, &netmask_addr, &gw_addr);
return NSAPI_ERROR_OK;
}
#endif /* LWIP_IPV4 */

#if LWIP_IPV6
ip6_addr_t ip_addr6;

conv_ip = inet6_aton(ip, &ip_addr6);
if (conv_ip) {
s8_t chosen_idx;
err_t ret = netif_add_ip6_address(&netif, &ip_addr6, &chosen_idx);
// If failed here, consider increasing LWIP_IPV6_NUM_ADDRESSES.
MBED_ASSERT(chosen_idx >= 0);
if (!ret) {
netif_ip6_addr_set_state(&netif, chosen_idx, ipv6_flag);
return NSAPI_ERROR_OK;
}
}
#endif /* LWIP_IPV6 */

return NSAPI_ERROR_PARAMETER;
}

nsapi_error_t LWIP::Interface::bringdown()
{
// Check if we've connected
Expand Down
33 changes: 33 additions & 0 deletions features/lwipstack/LWIPStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
#include "netsocket/OnboardNetworkStack.h"
#include "LWIPMemoryManager.h"

#if LWIP_IPV6
#include "lwip/ip6_addr.h"
#define IP_ADDR_VALID IP6_ADDR_VALID
#else
#define IP_ADDR_VALID 0
#endif

class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
public:
Expand All @@ -38,6 +44,33 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {

class Interface final : public OnboardNetworkStack::Interface {
public:
/** Set IP address to LWIP stack
*
* This function can set both IPv4 or IPv6 address to LWIP stack.
*
* bringup() can only take one IP address and in dual stack case
* another IP address can be set using this function.
*
* IPv4 or IPv6 address should be in format of https://tools.ietf.org/html/draft-main-ipaddr-text-rep-00.
*
* @param ip IP address to be used for the interface as IPv4 or IPv6 address string.
* This parameter should not be NULL.
* @param netmask Net mask to be used for the interface as IPv4 address string or NULL.
* NULL value will set this value to 255.255.255.255.
* This parameter will be ignored for IPv6 ip argument.
* @param gw Gateway address to be used for the interface as IPv4 address string or NULL
* NULL value will set this value to 0.0.0.0
* This parameter will be ignored for IPv6 ip argument.
* @param ipv6_flag Provide this flag for IPv6 state flag override. For example, you can set IP6_ADDR_PREFERRED.
* Default value is IP6_ADDR_VALID. For IPv4, this value will be ignored.
* @return NSAPI_ERROR_OK on success, or error code
*/
nsapi_error_t set_ip_address(const char *ip,
const char *netmask,
const char *gw,
uint8_t ipv6_flag = IP_ADDR_VALID
) override;

/** Connect the interface to the network
*
* Sets up a connection on specified network interface, using DHCP or provided network details. If the @a dhcp is set to
Expand Down
5 changes: 5 additions & 0 deletions features/netsocket/NetworkInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ nsapi_error_t NetworkInterface::add_dns_server(const SocketAddress &address, con
return get_stack()->add_dns_server(address, interface_name);
}

nsapi_error_t NetworkInterface::get_dns_server(int index, SocketAddress *address, const char *interface_name)
{
return get_stack()->get_dns_server(index, address, interface_name);
}

void NetworkInterface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
// Dummy, that needs to be overwritten when inherited, but cannot be removed
Expand Down
12 changes: 12 additions & 0 deletions features/netsocket/NetworkInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,18 @@ class NetworkInterface: public DNS {
*/
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name);

/** Get a domain name server from a list of servers to query
*
* Returns a DNS server address for a index. If returns error no more
* DNS servers to read.
*
* @param index Index of the DNS server, starts from zero
* @param address Destination for the host address
* @param interface_name Network interface name
* @return NSAPI_ERROR_OK on success, negative error code on failure
*/
virtual nsapi_error_t get_dns_server(int index, SocketAddress *address, const char *interface_name = NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the file, it was already changed to contain "override" rather than "virtual", please review the other methods and update this one as well

cc @kjbracey-arm was the design Mbed OS guidance updated and team as well to follow the latest changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a base class declaration so this needs to be virtual.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I had to opened a different file , can;t find it now.


/** Register callback for status reporting.
*
* The specified status callback function will be called on status changes
Expand Down
22 changes: 22 additions & 0 deletions features/netsocket/OnboardNetworkStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ class OnboardNetworkStack : public NetworkStack {
~Interface() = default;

public:
/** Set IP address
*
* bringup() can only take one IP address and in dual stack case
* another IP address can be set using this function.
*
* Must be called before bringup().
*
* @param ip IP address to be used for the interface as "W:X:Y:Z" or NULL
* @param netmask Net mask to be used for the interface as "W:X:Y:Z" or NULL
* @param gw Gateway address to be used for the interface as "W:X:Y:Z" or NULL
* @param ipv6_flag Provide this flag for IPv6 state flag override. For example, you can set IP6_ADDR_PREFERRED.
* For IPv4, this value will be ignored.
* @return NSAPI_ERROR_OK on success, or error code
*/
virtual nsapi_error_t set_ip_address(const char *ip,
const char *netmask,
const char *gw,
uint8_t ipv6_flag)
{
return NSAPI_ERROR_UNSUPPORTED;
}

/** Connect the interface to the network
*
* Sets up a connection on specified network interface, using DHCP or provided network details. If the @a dhcp is set to
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.