@@ -275,6 +275,37 @@ static const ip_addr_t *mbed_lwip_get_ipv6_addr(const struct netif *netif)
275
275
}
276
276
#endif
277
277
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
+
278
309
const ip_addr_t * mbed_lwip_get_ip_addr (bool any_addr , const struct netif * netif )
279
310
{
280
311
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
992
1023
return NSAPI_ERROR_PARAMETER ;
993
1024
}
994
1025
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 ;
1018
1028
}
1019
- #endif
1020
1029
1021
1030
err_t err = netconn_bind (s -> conn , & ip_addr , port );
1022
1031
return mbed_lwip_err_remap (err );
0 commit comments