From fbd0e1feaec252b4d20c1b819582cc5032dcd24d Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Fri, 8 Feb 2019 11:35:49 +0100 Subject: [PATCH] UdpContext::setMulticastInterface(): fix for IPv6 Per 'udp_set_multicast_netif_addr()' signature and comments in lwIP sources: An IPv4 address designating a specific interface must be used. When an IPv6 address is given, the matching IPv4 in the same interface must be selected. fix https://github.com/esp8266/Arduino/commit/e3bc3c226b4789f30e6f6a77a5522776b01f83bc#r32235572 --- cores/esp8266/AddrList.h | 1 + .../ESP8266WiFi/src/include/UdpContext.h | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cores/esp8266/AddrList.h b/cores/esp8266/AddrList.h index fd1ff45730..88bd8122bb 100644 --- a/cores/esp8266/AddrList.h +++ b/cores/esp8266/AddrList.h @@ -118,6 +118,7 @@ struct netifWrapper String toString() const { return addr().toString(); } // related to legacy address (_num=0, ipv4) + IPAddress ipv4 () const { return _netif->ip_addr; } IPAddress netmask () const { return _netif->netmask; } IPAddress gw () const { return _netif->gw; } diff --git a/libraries/ESP8266WiFi/src/include/UdpContext.h b/libraries/ESP8266WiFi/src/include/UdpContext.h index af67ae140a..e1be24c13b 100644 --- a/libraries/ESP8266WiFi/src/include/UdpContext.h +++ b/libraries/ESP8266WiFi/src/include/UdpContext.h @@ -30,6 +30,8 @@ void esp_schedule(); #include } +#include + #define GET_UDP_HDR(pb) (reinterpret_cast(((uint8_t*)((pb)->payload)) - UDP_HLEN)) class UdpContext @@ -107,6 +109,33 @@ class UdpContext udp_disconnect(_pcb); } +#if LWIP_IPV6 + + void setMulticastInterface(IPAddress addr) + { + // Per 'udp_set_multicast_netif_addr()' signature and comments + // in lwIP sources: + // An IPv4 address designating a specific interface must be used. + // When an IPv6 address is given, the matching IPv4 in the same + // interface must be selected. + + if (!addr.isV4()) + { + for (auto a: addrList) + if (a.addr() == addr) + { + // found the IPv6 address, + // redirect parameter to IPv4 address in this interface + addr = a.ipv4(); + break; + } + assert(addr.isV4()); + } + udp_set_multicast_netif_addr(_pcb, ip_2_ip4((const ip_addr_t*)addr)); + } + +#else // !LWIP_IPV6 + void setMulticastInterface(const IPAddress& addr) { #if LWIP_VERSION_MAJOR == 1 @@ -116,6 +145,8 @@ class UdpContext #endif } +#endif // !LWIP_IPV6 + void setMulticastTTL(int ttl) { #ifdef LWIP_MAYBE_XCC