Skip to content

Commit 52353d2

Browse files
committed
Add function to check if address is local
Update the code to check all addresses for all interfaces. Move the code from mbed_lwip_socket_bind() to a new function called mbed_lwip_is_local_addr()
1 parent 6d3e417 commit 52353d2

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

features/FEATURE_LWIP/lwip-interface/lwip_stack.c

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,37 @@ static const ip_addr_t *mbed_lwip_get_ipv6_addr(const struct netif *netif)
275275
}
276276
#endif
277277

278+
static bool mbed_lwip_is_local_addr(const ip_addr_t *ip_addr)
279+
{
280+
struct netif *netif;
281+
282+
for (netif = netif_list; netif != NULL; netif = netif->next) {
283+
if (!netif_is_up(netif)) {
284+
continue;
285+
}
286+
#if LWIP_IPV6
287+
if (IP_IS_V6(ip_addr)) {
288+
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
289+
if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) &&
290+
ip6_addr_cmp(netif_ip6_addr(netif, i), ip_addr)) {
291+
return true;
292+
}
293+
}
294+
}
295+
#endif
296+
297+
#if LWIP_IPV4
298+
if (IP_IS_V4(ip_addr)) {
299+
if (!ip4_addr_isany(netif_ip4_addr(netif)) &&
300+
ip4_addr_cmp(netif_ip_addr4(netif), ip_addr)) {
301+
return true;
302+
}
303+
}
304+
#endif
305+
}
306+
return false;
307+
}
308+
278309
const ip_addr_t *mbed_lwip_get_ip_addr(bool any_addr, const struct netif *netif)
279310
{
280311
const ip_addr_t *pref_ip_addr = 0;
@@ -992,31 +1023,9 @@ static nsapi_error_t mbed_lwip_socket_bind(nsapi_stack_t *stack, nsapi_socket_t
9921023
return NSAPI_ERROR_PARAMETER;
9931024
}
9941025

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-
}
1026+
if (!ip_addr_isany(&ip_addr) && !mbed_lwip_is_local_addr(&ip_addr)) {
1027+
return NSAPI_ERROR_PARAMETER;
10181028
}
1019-
#endif
10201029

10211030
err_t err = netconn_bind(s->conn, &ip_addr, port);
10221031
return mbed_lwip_err_remap(err);

0 commit comments

Comments
 (0)