From b272c5f14909705b19397263ec274e45b9e36507 Mon Sep 17 00:00:00 2001 From: Tymoteusz Bloch Date: Thu, 13 Jun 2019 16:34:50 +0200 Subject: [PATCH] Fixed LWIPStack socket_sendto member to fail if interface IP4/6 version differ from destination adress IP version --- features/lwipstack/LWIPStack.cpp | 12 +++++++++++- features/lwipstack/lwip_tools.cpp | 11 +++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/features/lwipstack/LWIPStack.cpp b/features/lwipstack/LWIPStack.cpp index 04ca9d59c96..664337b5ab7 100644 --- a/features/lwipstack/LWIPStack.cpp +++ b/features/lwipstack/LWIPStack.cpp @@ -34,6 +34,7 @@ #include "lwip/dns.h" #include "lwip/udp.h" #include "lwip/raw.h" +#include "lwip/netif.h" #include "lwip/lwip_errno.h" #include "lwip-sys/arch/sys_arch.h" @@ -493,7 +494,16 @@ nsapi_size_or_error_t LWIP::socket_sendto(nsapi_socket_t handle, const SocketAdd if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) { return NSAPI_ERROR_PARAMETER; } - + struct netif *netif_ = netif_get_by_index(s->conn->pcb.ip->netif_idx); + if (!netif_) { + netif_ = &default_interface->netif; + } + if (netif_) { + if ((addr.version == NSAPI_IPv4 && !get_ipv4_addr(netif_)) || + (addr.version == NSAPI_IPv6 && !get_ipv6_addr(netif_))) { + return NSAPI_ERROR_PARAMETER; + } + } struct netbuf *buf = netbuf_new(); err_t err = netbuf_ref(buf, data, (u16_t)size); diff --git a/features/lwipstack/lwip_tools.cpp b/features/lwipstack/lwip_tools.cpp index 29e6bbb7305..c3514cacfc9 100644 --- a/features/lwipstack/lwip_tools.cpp +++ b/features/lwipstack/lwip_tools.cpp @@ -59,9 +59,10 @@ nsapi_error_t LWIP::err_remap(err_t err) } } -#if LWIP_IPV4 + const ip_addr_t *LWIP::get_ipv4_addr(const struct netif *netif) { +#if LWIP_IPV4 if (!netif_is_up(netif)) { return NULL; } @@ -69,14 +70,13 @@ const ip_addr_t *LWIP::get_ipv4_addr(const struct netif *netif) if (!ip4_addr_isany(netif_ip4_addr(netif))) { return netif_ip_addr4(netif); } - +#endif return NULL; } -#endif -#if LWIP_IPV6 const ip_addr_t *LWIP::get_ipv6_addr(const struct netif *netif) { +#if LWIP_IPV6 if (!netif_is_up(netif)) { return NULL; } @@ -87,10 +87,9 @@ const ip_addr_t *LWIP::get_ipv6_addr(const struct netif *netif) return netif_ip_addr6(netif, i); } } - +#endif return NULL; } -#endif bool LWIP::is_local_addr(const ip_addr_t *ip_addr) {