Skip to content

Commit 6d3e417

Browse files
committed
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
1 parent dd5bd73 commit 6d3e417

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

features/FEATURE_LWIP/lwip-interface/lwip_stack.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,32 @@ static nsapi_error_t mbed_lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t
992992
return NSAPI_ERROR_PARAMETER;
993993
}
994994

995+
#if LWIP_IPV6
996+
if (IP_IS_V6(ip_addr) && !ip6_addr_isany(&ip_addr)) {
997+
const ip_addr_t *local_addr = mbed_lwip_get_ipv6_addr(&lwip_netif);
998+
if (!local_addr) {
999+
return NSAPI_ERROR_PARAMETER;
1000+
}
1001+
1002+
if (!ip6_addr_cmp(local_addr, &ip_addr)) {
1003+
return NSAPI_ERROR_PARAMETER;
1004+
}
1005+
}
1006+
#endif
1007+
1008+
#if LWIP_IPV4
1009+
if (IP_IS_V4(ip_addr) && !ip4_addr_isany(&ip_addr)) {
1010+
const ip_addr_t *local_addr = mbed_lwip_get_ipv4_addr(&lwip_netif);
1011+
if (!local_addr) {
1012+
return NSAPI_ERROR_PARAMETER;
1013+
}
1014+
1015+
if (!ip4_addr_cmp(local_addr, &ip_addr)) {
1016+
return NSAPI_ERROR_PARAMETER;
1017+
}
1018+
}
1019+
#endif
1020+
9951021
err_t err = netconn_bind(s->conn, &ip_addr, port);
9961022
return mbed_lwip_err_remap(err);
9971023
}
@@ -1000,6 +1026,10 @@ static nsapi_error_t mbed_lwip_socket_listen(nsapi_stack_t *stack, nsapi_socket_
10001026
{
10011027
struct lwip_socket *s = (struct lwip_socket *)handle;
10021028

1029+
if (s->conn->pcb.tcp->local_port == 0) {
1030+
return NSAPI_ERROR_PARAMETER;
1031+
}
1032+
10031033
err_t err = netconn_listen_with_backlog(s->conn, backlog);
10041034
return mbed_lwip_err_remap(err);
10051035
}
@@ -1028,6 +1058,10 @@ static nsapi_error_t mbed_lwip_socket_accept(nsapi_stack_t *stack, nsapi_socket_
10281058
return NSAPI_ERROR_NO_SOCKET;
10291059
}
10301060

1061+
if (s->conn->pcb.tcp->state != LISTEN) {
1062+
return NSAPI_ERROR_PARAMETER;
1063+
}
1064+
10311065
err_t err = netconn_accept(s->conn, &ns->conn);
10321066
if (err != ERR_OK) {
10331067
mbed_lwip_arena_dealloc(ns);

0 commit comments

Comments
 (0)