From 5987dabbd6a79fc4c5e1638eac45298683fbbf09 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 18 Oct 2021 12:02:49 -0400 Subject: [PATCH] Better IPv6 handling of AddrAny (which maps to IPv4 if enabled in lwip) (#10564) * Better IPv6 handling of any: support both LWIP conversion if IPv6 is disabled and enabled * Remove previous settype code * Fix typo --- src/inet/IPAddress.cpp | 4 ++++ src/inet/UDPEndPoint.cpp | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/inet/IPAddress.cpp b/src/inet/IPAddress.cpp index 0f2ffa69fa773c..019673a4faf36f 100644 --- a/src/inet/IPAddress.cpp +++ b/src/inet/IPAddress.cpp @@ -90,7 +90,11 @@ ip_addr_t IPAddress::ToLwIPAddr(void) const break; default: +#if INET_CONFIG_ENABLE_IPV4 ret = *IP_ADDR_ANY; +#else + ret = *IP6_ADDR_ANY; +#endif break; } diff --git a/src/inet/UDPEndPoint.cpp b/src/inet/UDPEndPoint.cpp index d4921fd752e01a..f5a88f353565eb 100644 --- a/src/inet/UDPEndPoint.cpp +++ b/src/inet/UDPEndPoint.cpp @@ -134,10 +134,19 @@ CHIP_ERROR UDPEndPoint::BindImpl(IPAddressType addrType, const IPAddress & addr, { #if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5 ip_addr_t ipAddr = addr.ToLwIPAddr(); -#if INET_CONFIG_ENABLE_IPV4 - lwip_ip_addr_type lType = IPAddress::ToLwIPAddrType(addrType); - IP_SET_TYPE_VAL(ipAddr, lType); -#endif // INET_CONFIG_ENABLE_IPV4 + + // TODO: IPAddress ANY has only one constant state, however addrType + // has separate IPV4 and IPV6 'any' settings. This tries to correct + // for this as LWIP default if IPv4 is compiled in is to consider + // 'any == any_v4' + // + // We may want to consider having separate AnyV4 and AnyV6 constants + // inside CHIP to resolve this ambiguity + if ((addr.Type() == kIPAddressType_Any) && (addrType == kIPAddressType_IPv6)) + { + ipAddr = *IP6_ADDR_ANY; + } + res = chip::System::MapErrorLwIP(udp_bind(mUDP, &ipAddr, port)); #else // LWIP_VERSION_MAJOR <= 1 && LWIP_VERSION_MINOR < 5 if (addrType == kIPAddressType_IPv6)