Skip to content

lwip - Add checks for invalid state of network #2561

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 1 commit into from
Sep 16, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ int EthernetInterface::connect()

int EthernetInterface::disconnect()
{
lwip_bringdown();
return 0;
return lwip_bringdown();
}

const char *EthernetInterface::get_ip_address()
Expand Down
20 changes: 19 additions & 1 deletion features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ const char *lwip_get_ip_address(void)
int lwip_bringup(void)
{
// Check if we've already connected
if (lwip_get_ip_address()) {
return NSAPI_ERROR_PARAMETER;
}

// Check if we've already brought up lwip
if (!lwip_get_mac_address()) {
// Set up network
lwip_set_mac_address();
Expand Down Expand Up @@ -181,12 +186,19 @@ int lwip_bringup(void)
return 0;
}

void lwip_bringdown(void)
int lwip_bringdown(void)
{
// Check if we've connected
if (!lwip_get_ip_address()) {
return NSAPI_ERROR_PARAMETER;
Copy link
Contributor

@bogdanm bogdanm Sep 9, 2016

Choose a reason for hiding this comment

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

Isn't this supposed to be return NSAPI_ERROR_NO_CONNECTION (like below)?

Copy link
Contributor Author

@geky geky Sep 9, 2016

Choose a reason for hiding this comment

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

It could be, there's not much precedent here that I am aware of.

I chose NSAPI_ERROR_PARAMETER to match the error on connecting an already connected network interface (L148). Disconnecting an already disconnected interface is more a user error than a network error. Do you think this should be NSAPI_ERROR_NO_CONNECTION?

}

// Disconnect from the network
dhcp_release(&lwip_netif);
dhcp_stop(&lwip_netif);
lwip_ip_addr[0] = '\0';

return 0;
}


Expand Down Expand Up @@ -231,6 +243,12 @@ static nsapi_addr_t lwip_get_addr(nsapi_stack_t *stack)

static int lwip_socket_open(nsapi_stack_t *stack, nsapi_socket_t *handle, nsapi_protocol_t proto)
{
// check if network is connected
if (!lwip_get_ip_address()) {
return NSAPI_ERROR_NO_CONNECTION;
}

// allocate a socket
struct lwip_socket *s = lwip_arena_alloc();
if (!s) {
return NSAPI_ERROR_NO_SOCKET;
Expand Down
2 changes: 1 addition & 1 deletion features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extern "C" {

// Access to lwip through the nsapi
int lwip_bringup(void);
void lwip_bringdown(void);
int lwip_bringdown(void);

extern nsapi_stack_t lwip_stack;

Expand Down