Skip to content

Commit 4c444ae

Browse files
authored
Merge pull request #12606 from kivaisan/netsocket_set_ip_address_and_get_dns_server
Netsocket: Introduce set_ip_address and get_dns_server APIs
2 parents 098c72a + b7b0fbd commit 4c444ae

File tree

12 files changed

+134
-15
lines changed

12 files changed

+134
-15
lines changed

UNITTESTS/stubs/NetworkInterface_stub.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ nsapi_error_t NetworkInterface::add_dns_server(const SocketAddress &address, con
7272
return NSAPI_ERROR_UNSUPPORTED;
7373
}
7474

75+
nsapi_error_t NetworkInterface::get_dns_server(int index, SocketAddress *address, const char *interface_name)
76+
{
77+
return NSAPI_ERROR_UNSUPPORTED;
78+
}
79+
7580
void NetworkInterface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
7681
{
7782

features/lwipstack/LWIPInterface.cpp

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -680,23 +680,13 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
680680
}
681681
#endif /* LWIP_IPV6 */
682682

683-
#if LWIP_IPV4
684-
if (stack != IPV6_STACK) {
685-
if (!dhcp && !ppp_enabled) {
686-
ip4_addr_t ip_addr;
687-
ip4_addr_t netmask_addr;
688-
ip4_addr_t gw_addr;
689-
690-
if (!inet_aton(ip, &ip_addr) ||
691-
!inet_aton(netmask, &netmask_addr) ||
692-
!inet_aton(gw, &gw_addr)) {
693-
return NSAPI_ERROR_PARAMETER;
694-
}
695-
696-
netif_set_addr(&netif, &ip_addr, &netmask_addr, &gw_addr);
683+
nsapi_error_t ret_add_ip = NSAPI_ERROR_OK;
684+
if (!dhcp && !ppp_enabled) {
685+
ret_add_ip = set_ip_address(ip, netmask, gw);
686+
if (ret_add_ip != NSAPI_ERROR_OK) {
687+
return ret_add_ip;
697688
}
698689
}
699-
#endif
700690

701691
if (client_callback) {
702692
client_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_CONNECTING);
@@ -753,6 +743,58 @@ nsapi_error_t LWIP::Interface::bringup(bool dhcp, const char *ip, const char *ne
753743
return NSAPI_ERROR_OK;
754744
}
755745

746+
nsapi_error_t LWIP::Interface::set_ip_address(const char *ip,
747+
const char *netmask,
748+
const char *gw,
749+
uint8_t ipv6_flag)
750+
{
751+
if (!ip) {
752+
return NSAPI_ERROR_PARAMETER;
753+
}
754+
755+
int conv_ip = 1;
756+
757+
#if LWIP_IPV4
758+
ip4_addr_t ip_addr;
759+
ip4_addr_t netmask_addr;
760+
ip4_addr_t gw_addr;
761+
IP4_ADDR(&netmask_addr, 255, 255, 255, 255);
762+
ip4_addr_set_zero(&gw_addr);
763+
int conv_netmask = 1;
764+
int conv_gw = 1;
765+
766+
conv_ip = inet_aton(ip, &ip_addr);
767+
if (netmask) {
768+
conv_netmask = inet_aton(netmask, &netmask_addr);
769+
}
770+
if (gw) {
771+
conv_gw = inet_aton(gw, &gw_addr);
772+
}
773+
if (conv_ip && conv_netmask && conv_gw) {
774+
netif_set_addr(&netif, &ip_addr, &netmask_addr, &gw_addr);
775+
return NSAPI_ERROR_OK;
776+
}
777+
#endif /* LWIP_IPV4 */
778+
779+
#if LWIP_IPV6
780+
ip6_addr_t ip_addr6;
781+
782+
conv_ip = inet6_aton(ip, &ip_addr6);
783+
if (conv_ip) {
784+
s8_t chosen_idx;
785+
err_t ret = netif_add_ip6_address(&netif, &ip_addr6, &chosen_idx);
786+
// If failed here, consider increasing LWIP_IPV6_NUM_ADDRESSES.
787+
MBED_ASSERT(chosen_idx >= 0);
788+
if (!ret) {
789+
netif_ip6_addr_set_state(&netif, chosen_idx, ipv6_flag);
790+
return NSAPI_ERROR_OK;
791+
}
792+
}
793+
#endif /* LWIP_IPV6 */
794+
795+
return NSAPI_ERROR_PARAMETER;
796+
}
797+
756798
nsapi_error_t LWIP::Interface::bringdown()
757799
{
758800
// Check if we've connected

features/lwipstack/LWIPStack.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
#include "netsocket/OnboardNetworkStack.h"
3131
#include "LWIPMemoryManager.h"
3232

33+
#if LWIP_IPV6
34+
#include "lwip/ip6_addr.h"
35+
#define IP_ADDR_VALID IP6_ADDR_VALID
36+
#else
37+
#define IP_ADDR_VALID 0
38+
#endif
3339

3440
class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
3541
public:
@@ -38,6 +44,33 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
3844

3945
class Interface final : public OnboardNetworkStack::Interface {
4046
public:
47+
/** Set IP address to LWIP stack
48+
*
49+
* This function can set both IPv4 or IPv6 address to LWIP stack.
50+
*
51+
* bringup() can only take one IP address and in dual stack case
52+
* another IP address can be set using this function.
53+
*
54+
* IPv4 or IPv6 address should be in format of https://tools.ietf.org/html/draft-main-ipaddr-text-rep-00.
55+
*
56+
* @param ip IP address to be used for the interface as IPv4 or IPv6 address string.
57+
* This parameter should not be NULL.
58+
* @param netmask Net mask to be used for the interface as IPv4 address string or NULL.
59+
* NULL value will set this value to 255.255.255.255.
60+
* This parameter will be ignored for IPv6 ip argument.
61+
* @param gw Gateway address to be used for the interface as IPv4 address string or NULL
62+
* NULL value will set this value to 0.0.0.0
63+
* This parameter will be ignored for IPv6 ip argument.
64+
* @param ipv6_flag Provide this flag for IPv6 state flag override. For example, you can set IP6_ADDR_PREFERRED.
65+
* Default value is IP6_ADDR_VALID. For IPv4, this value will be ignored.
66+
* @return NSAPI_ERROR_OK on success, or error code
67+
*/
68+
nsapi_error_t set_ip_address(const char *ip,
69+
const char *netmask,
70+
const char *gw,
71+
uint8_t ipv6_flag = IP_ADDR_VALID
72+
) override;
73+
4174
/** Connect the interface to the network
4275
*
4376
* Sets up a connection on specified network interface, using DHCP or provided network details. If the @a dhcp is set to

features/netsocket/NetworkInterface.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ nsapi_error_t NetworkInterface::add_dns_server(const SocketAddress &address, con
101101
return get_stack()->add_dns_server(address, interface_name);
102102
}
103103

104+
nsapi_error_t NetworkInterface::get_dns_server(int index, SocketAddress *address, const char *interface_name)
105+
{
106+
return get_stack()->get_dns_server(index, address, interface_name);
107+
}
108+
104109
void NetworkInterface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
105110
{
106111
// Dummy, that needs to be overwritten when inherited, but cannot be removed

features/netsocket/NetworkInterface.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ class NetworkInterface: public DNS {
324324
*/
325325
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name);
326326

327+
/** Get a domain name server from a list of servers to query
328+
*
329+
* Returns a DNS server address for a index. If returns error no more
330+
* DNS servers to read.
331+
*
332+
* @param index Index of the DNS server, starts from zero
333+
* @param address Destination for the host address
334+
* @param interface_name Network interface name
335+
* @return NSAPI_ERROR_OK on success, negative error code on failure
336+
*/
337+
virtual nsapi_error_t get_dns_server(int index, SocketAddress *address, const char *interface_name = NULL);
338+
327339
/** Register callback for status reporting.
328340
*
329341
* The specified status callback function will be called on status changes

features/netsocket/OnboardNetworkStack.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ class OnboardNetworkStack : public NetworkStack {
5151
~Interface() = default;
5252

5353
public:
54+
/** Set IP address
55+
*
56+
* bringup() can only take one IP address and in dual stack case
57+
* another IP address can be set using this function.
58+
*
59+
* Must be called before bringup().
60+
*
61+
* @param ip IP address to be used for the interface as "W:X:Y:Z" or NULL
62+
* @param netmask Net mask to be used for the interface as "W:X:Y:Z" or NULL
63+
* @param gw Gateway address to be used for the interface as "W:X:Y:Z" or NULL
64+
* @param ipv6_flag Provide this flag for IPv6 state flag override. For example, you can set IP6_ADDR_PREFERRED.
65+
* For IPv4, this value will be ignored.
66+
* @return NSAPI_ERROR_OK on success, or error code
67+
*/
68+
virtual nsapi_error_t set_ip_address(const char *ip,
69+
const char *netmask,
70+
const char *gw,
71+
uint8_t ipv6_flag)
72+
{
73+
return NSAPI_ERROR_UNSUPPORTED;
74+
}
75+
5476
/** Connect the interface to the network
5577
*
5678
* Sets up a connection on specified network interface, using DHCP or provided network details. If the @a dhcp is set to
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)