Skip to content

Commit

Permalink
fix(IPAddress): TryParse scoped ipv6 addressess for addresses enclose…
Browse files Browse the repository at this point in the history
…d in [ ] #4644
  • Loading branch information
aleks-f committed Oct 30, 2024
1 parent 6dadf9a commit c77d021
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Net/src/IPAddressImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ IPv6AddressImpl IPv6AddressImpl::parse(const std::string& addr)
{
std::string::size_type start = ('[' == addr[0]) ? 1 : 0;
std::string unscopedAddr(addr, start, pos - start);
std::string scope(addr, pos + 1, addr.size() - start - pos);
std::string scope(addr, pos + 1, addr.size() - (2*start) - pos);
Poco::UInt32 scopeId(0);
if (!(scopeId = if_nametoindex(scope.c_str())))
return IPv6AddressImpl();
Expand Down
73 changes: 71 additions & 2 deletions Net/testsuite/src/IPAddressTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/IPAddress.h"
#include "Poco/Net/NetworkInterface.h"
#include "Poco/Net/NetException.h"
#include "Poco/Format.h"
#include <iostream>


using Poco::Net::IPAddress;
using Poco::Net::NetworkInterface;
using Poco::Net::InvalidAddressException;


Expand All @@ -35,7 +39,7 @@ void IPAddressTest::testStringConv()
IPAddress ia1(std::move(ia01));
assertTrue (ia1.family() == IPAddress::IPv4);
assertTrue (ia1.toString() == "127.0.0.1");

IPAddress ia02 = IPAddress("192.168.1.120");
IPAddress ia2(std::move(ia02));
assertTrue (ia2.family() == IPAddress::IPv4);
Expand Down Expand Up @@ -67,7 +71,7 @@ void IPAddressTest::testStringConv6()
IPAddress ia1(std::move(ia01));
assertTrue (ia1.family() == IPAddress::IPv6);
assertTrue (ia1.toString() == "1080::8:600:200a:425c");

IPAddress ia02 = IPAddress("1080::8:600:200A:425C");
IPAddress ia2(std::move(ia02));
assertTrue (ia2.family() == IPAddress::IPv6);
Expand Down Expand Up @@ -424,6 +428,44 @@ void IPAddressTest::testClassification6()
assertTrue (!ip10.isOrgLocalMC());
assertTrue (!ip10.isGlobalMC());

NetworkInterface::Map m = NetworkInterface::map(false, false);
for (auto it = m.begin(); it != m.end(); ++it)
{
IPAddress ip11(Poco::format("fe80::1592:96a0:88bf:d2d7%%%s",
it->second.adapterName())); // link local unicast scoped
assertEqual (ip11.scope(), it->second.index());
assertTrue (!ip11.isWildcard());
assertTrue (!ip11.isBroadcast());
assertTrue (!ip11.isLoopback());
assertTrue (!ip11.isMulticast());
assertTrue (ip11.isUnicast());
assertTrue (ip11.isLinkLocal());
assertTrue (!ip11.isSiteLocal());
assertTrue (!ip11.isWellKnownMC());
assertTrue (!ip11.isNodeLocalMC());
assertTrue (!ip11.isLinkLocalMC());
assertTrue (!ip11.isSiteLocalMC());
assertTrue (!ip11.isOrgLocalMC());
assertTrue (!ip11.isGlobalMC());

IPAddress ip12(Poco::format("[fe80::1592:96a0:88bf:d2d7%%%s]",
it->second.adapterName())); // link local unicast scoped
assertEqual (ip12.scope(), it->second.index());
assertTrue (!ip12.isWildcard());
assertTrue (!ip12.isBroadcast());
assertTrue (!ip12.isLoopback());
assertTrue (!ip12.isMulticast());
assertTrue (ip12.isUnicast());
assertTrue (ip12.isLinkLocal());
assertTrue (!ip12.isSiteLocal());
assertTrue (!ip12.isWellKnownMC());
assertTrue (!ip12.isNodeLocalMC());
assertTrue (!ip12.isLinkLocalMC());
assertTrue (!ip12.isSiteLocalMC());
assertTrue (!ip12.isOrgLocalMC());
assertTrue (!ip12.isGlobalMC());
}

IPAddress ip6("fec0::21f:5bff:fec6:6707"); // site local unicast (RFC 4291)
assertTrue (!ip6.isWildcard());
assertTrue (!ip6.isBroadcast());
Expand Down Expand Up @@ -690,6 +732,32 @@ void IPAddressTest::testByteOrderMacros()
}


void IPAddressTest::testScoped()
{
#ifdef POCO_HAVE_IPv6
NetworkInterface::Map m = NetworkInterface::map(false, false);
if (m.size() == 0)
{
std::cout << "No network interfaces found." << std::endl;
return;
}

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();
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
}


void IPAddressTest::setUp()
{
}
Expand Down Expand Up @@ -719,6 +787,7 @@ CppUnit::Test* IPAddressTest::suite()
CppUnit_addTest(pSuite, IPAddressTest, testPrefixLen);
CppUnit_addTest(pSuite, IPAddressTest, testOperators);
CppUnit_addTest(pSuite, IPAddressTest, testByteOrderMacros);
CppUnit_addTest(pSuite, IPAddressTest, testScoped);

return pSuite;
}
1 change: 1 addition & 0 deletions Net/testsuite/src/IPAddressTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class IPAddressTest: public CppUnit::TestCase
void testPrefixLen();
void testOperators();
void testByteOrderMacros();
void testScoped();

void setUp();
void tearDown();
Expand Down

0 comments on commit c77d021

Please sign in to comment.