From 13ea31f442d1ecabec0b63a58e6dc21b04949eae Mon Sep 17 00:00:00 2001
From: Alex Fabijanic <alex@pocoproject.org>
Date: Wed, 30 Oct 2024 22:16:27 -0500
Subject: [PATCH] fix(IPAddress): windows scoped test #4644

---
 Net/src/IPAddressImpl.cpp           | 7 ++++++-
 Net/testsuite/src/IPAddressTest.cpp | 9 +++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/Net/src/IPAddressImpl.cpp b/Net/src/IPAddressImpl.cpp
index deccbd3c2..693f6f43b 100644
--- a/Net/src/IPAddressImpl.cpp
+++ b/Net/src/IPAddressImpl.cpp
@@ -658,10 +658,15 @@ IPv6AddressImpl IPv6AddressImpl::parse(const std::string& addr)
 	struct addrinfo hints;
 	std::memset(&hints, 0, sizeof(hints));
 	hints.ai_flags = AI_NUMERICHOST;
+	// for the reason why this is not AF_INET6, see
+	// https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/getaddrinfo-fails-error-11001-call-af-inet6-family
+	hints.ai_family = AF_UNSPEC;
 	int rc = getaddrinfo(addr.c_str(), NULL, &hints, &pAI);
 	if (rc == 0)
 	{
-		IPv6AddressImpl result = IPv6AddressImpl(&reinterpret_cast<struct sockaddr_in6*>(pAI->ai_addr)->sin6_addr, static_cast<int>(reinterpret_cast<struct sockaddr_in6*>(pAI->ai_addr)->sin6_scope_id));
+		IPv6AddressImpl result = IPv6AddressImpl(
+			&reinterpret_cast<struct sockaddr_in6*>(pAI->ai_addr)->sin6_addr,
+			static_cast<int>(reinterpret_cast<struct sockaddr_in6*>(pAI->ai_addr)->sin6_scope_id));
 		freeaddrinfo(pAI);
 		return result;
 	}
diff --git a/Net/testsuite/src/IPAddressTest.cpp b/Net/testsuite/src/IPAddressTest.cpp
index 3981d3d51..73a4cff41 100644
--- a/Net/testsuite/src/IPAddressTest.cpp
+++ b/Net/testsuite/src/IPAddressTest.cpp
@@ -745,14 +745,19 @@ void IPAddressTest::testScoped()
 	IPAddress ip;
 	assertFalse (IPAddress::tryParse("fe80::1592:96a0:88bf:d2d7%xyzabc123", ip));
 
-	std::string scope;
 	auto it = m.begin();
 	auto end = m.end();
 	for (; it != end; ++it)
 	{
-		scope = it->second.adapterName();
+#if defined(_WIN32)
+		int scope = it->second.index();
+		assertTrue(IPAddress::tryParse(Poco::format("[fe80::1592:96a0:88bf:d2d7%%%d]", scope), ip));
+		assertTrue(IPAddress::tryParse(Poco::format("fe80::1592:96a0:88bf:d2d7%%%d", scope), ip));
+#else
+		std::string scope = it->second.adapterName();
 		assertTrue (IPAddress::tryParse(Poco::format("[fe80::1592:96a0:88bf:d2d7%%%s]", scope), ip));
 		assertTrue (IPAddress::tryParse(Poco::format("fe80::1592:96a0:88bf:d2d7%%%s", scope), ip));
+#endif
 	}
 #endif
 }