lwip: fix socket behaviour

Return NSAPI_ERROR_PARAMETER when:
Binding to a non-local address
Socket listen() is called without calling bind() first
Socket accept() is called without calling listen() first
pull/5684/head
Juha Ylinen 2017-12-11 15:52:42 +02:00
parent dd5bd73a88
commit 6d3e417eaa
1 changed files with 34 additions and 0 deletions

View File

@ -992,6 +992,32 @@ static nsapi_error_t mbed_lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t
return NSAPI_ERROR_PARAMETER;
}
#if LWIP_IPV6
if (IP_IS_V6(ip_addr) && !ip6_addr_isany(&ip_addr)) {
const ip_addr_t *local_addr = mbed_lwip_get_ipv6_addr(&lwip_netif);
if (!local_addr) {
return NSAPI_ERROR_PARAMETER;
}
if (!ip6_addr_cmp(local_addr, &ip_addr)) {
return NSAPI_ERROR_PARAMETER;
}
}
#endif
#if LWIP_IPV4
if (IP_IS_V4(ip_addr) && !ip4_addr_isany(&ip_addr)) {
const ip_addr_t *local_addr = mbed_lwip_get_ipv4_addr(&lwip_netif);
if (!local_addr) {
return NSAPI_ERROR_PARAMETER;
}
if (!ip4_addr_cmp(local_addr, &ip_addr)) {
return NSAPI_ERROR_PARAMETER;
}
}
#endif
err_t err = netconn_bind(s->conn, &ip_addr, port);
return mbed_lwip_err_remap(err);
}
@ -1000,6 +1026,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);
}
@ -1028,6 +1058,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);