Skip to content

lwip: fix socket behaviour #5684

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 28, 2017
Merged
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
43 changes: 43 additions & 0 deletions features/FEATURE_LWIP/lwip-interface/lwip_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,37 @@ static const ip_addr_t *mbed_lwip_get_ipv6_addr(const struct netif *netif)
}
#endif

static bool mbed_lwip_is_local_addr(const ip_addr_t *ip_addr)
{
struct netif *netif;

for (netif = netif_list; netif != NULL; netif = netif->next) {
if (!netif_is_up(netif)) {
continue;
}
#if LWIP_IPV6
if (IP_IS_V6(ip_addr)) {
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
ip6_addr_cmp(netif_ip6_addr(netif, i), ip_2_ip6(ip_addr))) {
return true;
}
}
}
#endif

#if LWIP_IPV4
if (IP_IS_V4(ip_addr)) {
if (!ip4_addr_isany(netif_ip4_addr(netif)) &&
ip4_addr_cmp(netif_ip4_addr(netif), ip_2_ip4(ip_addr))) {
return true;
}
}
#endif
}
return false;
}

const ip_addr_t *mbed_lwip_get_ip_addr(bool any_addr, const struct netif *netif)
{
const ip_addr_t *pref_ip_addr = 0;
Expand Down Expand Up @@ -992,6 +1023,10 @@ static nsapi_error_t mbed_lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t
return NSAPI_ERROR_PARAMETER;
}

if (!ip_addr_isany(&ip_addr) && !mbed_lwip_is_local_addr(&ip_addr)) {
return NSAPI_ERROR_PARAMETER;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if the return error here should be "NO_ADDRESS"? Guess this is fine though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see Nanostack has "PARAMETER" already, so this matches.

}

err_t err = netconn_bind(s->conn, &ip_addr, port);
return mbed_lwip_err_remap(err);
}
Expand All @@ -1000,6 +1035,10 @@ static nsapi_error_t mbed_lwip_socket_listen(nsapi_stack_t *stack, nsapi_socket_
{
struct lwip_socket *s = (struct lwip_socket *)handle;

if (s->conn->pcb.tcp->local_port == 0) {
return NSAPI_ERROR_PARAMETER;
}

err_t err = netconn_listen_with_backlog(s->conn, backlog);
return mbed_lwip_err_remap(err);
}
Expand Down Expand Up @@ -1028,6 +1067,10 @@ static nsapi_error_t mbed_lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_
return NSAPI_ERROR_NO_SOCKET;
}

if (s->conn->pcb.tcp->state != LISTEN) {
return NSAPI_ERROR_PARAMETER;
}

err_t err = netconn_accept(s->conn, &ns->conn);
if (err != ERR_OK) {
mbed_lwip_arena_dealloc(ns);
Expand Down