From 5bac3e303faecd7feee2c57f6daab1c2120d4711 Mon Sep 17 00:00:00 2001 From: Alex Fabijanic Date: Wed, 18 May 2022 15:04:09 -0700 Subject: [PATCH] fix(IPAddress): IPAddress::isLoopback() returns false for IPv4 mapped in IPv6 #3399 --- Net/src/IPAddressImpl.cpp | 3 +++ Net/testsuite/src/IPAddressTest.cpp | 30 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Net/src/IPAddressImpl.cpp b/Net/src/IPAddressImpl.cpp index 4b20b1dbb..03b55ec45 100644 --- a/Net/src/IPAddressImpl.cpp +++ b/Net/src/IPAddressImpl.cpp @@ -564,6 +564,9 @@ bool IPv6AddressImpl::isBroadcast() const bool IPv6AddressImpl::isLoopback() const { + if (isIPv4Mapped()) + return (ByteOrder::fromNetwork(_addr.s6_addr[6]) & 0xFF000000) == 0x7F000000; + const UInt16* words = reinterpret_cast(&_addr); return words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && words[4] == 0 && words[5] == 0 && words[6] == 0 && ByteOrder::fromNetwork(words[7]) == 0x0001; diff --git a/Net/testsuite/src/IPAddressTest.cpp b/Net/testsuite/src/IPAddressTest.cpp index 2f317a9cc..ea5f87bd2 100644 --- a/Net/testsuite/src/IPAddressTest.cpp +++ b/Net/testsuite/src/IPAddressTest.cpp @@ -453,6 +453,36 @@ void IPAddressTest::testClassification6() assertTrue (!ip7.isSiteLocalMC()); assertTrue (!ip7.isOrgLocalMC()); assertTrue (!ip7.isGlobalMC()); + + IPAddress ip8("::ffff:127.0.0.1"); // IPv4-mapped loopback + assertTrue (!ip3.isWildcard()); + assertTrue (!ip3.isBroadcast()); + assertTrue (ip3.isLoopback()); + assertTrue (!ip3.isMulticast()); + assertTrue (ip3.isUnicast()); + assertTrue (!ip3.isLinkLocal()); + assertTrue (!ip3.isSiteLocal()); + assertTrue (!ip3.isWellKnownMC()); + assertTrue (!ip3.isNodeLocalMC()); + assertTrue (!ip3.isLinkLocalMC()); + assertTrue (!ip3.isSiteLocalMC()); + assertTrue (!ip3.isOrgLocalMC()); + assertTrue (!ip3.isGlobalMC()); + + IPAddress ip9("::ffff:127.255.255.254"); // IPv4-mapped loopback + assertTrue (!ip3.isWildcard()); + assertTrue (!ip3.isBroadcast()); + assertTrue (ip3.isLoopback()); + assertTrue (!ip3.isMulticast()); + assertTrue (ip3.isUnicast()); + assertTrue (!ip3.isLinkLocal()); + assertTrue (!ip3.isSiteLocal()); + assertTrue (!ip3.isWellKnownMC()); + assertTrue (!ip3.isNodeLocalMC()); + assertTrue (!ip3.isLinkLocalMC()); + assertTrue (!ip3.isSiteLocalMC()); + assertTrue (!ip3.isOrgLocalMC()); + assertTrue (!ip3.isGlobalMC()); #endif }