Skip to content

Commit c77d021

Browse files
committed
fix(IPAddress): TryParse scoped ipv6 addressess for addresses enclosed in [ ] #4644
1 parent 6dadf9a commit c77d021

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

Net/src/IPAddressImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ IPv6AddressImpl IPv6AddressImpl::parse(const std::string& addr)
673673
{
674674
std::string::size_type start = ('[' == addr[0]) ? 1 : 0;
675675
std::string unscopedAddr(addr, start, pos - start);
676-
std::string scope(addr, pos + 1, addr.size() - start - pos);
676+
std::string scope(addr, pos + 1, addr.size() - (2*start) - pos);
677677
Poco::UInt32 scopeId(0);
678678
if (!(scopeId = if_nametoindex(scope.c_str())))
679679
return IPv6AddressImpl();

Net/testsuite/src/IPAddressTest.cpp

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
#include "CppUnit/TestCaller.h"
1313
#include "CppUnit/TestSuite.h"
1414
#include "Poco/Net/IPAddress.h"
15+
#include "Poco/Net/NetworkInterface.h"
1516
#include "Poco/Net/NetException.h"
17+
#include "Poco/Format.h"
18+
#include <iostream>
1619

1720

1821
using Poco::Net::IPAddress;
22+
using Poco::Net::NetworkInterface;
1923
using Poco::Net::InvalidAddressException;
2024

2125

@@ -35,7 +39,7 @@ void IPAddressTest::testStringConv()
3539
IPAddress ia1(std::move(ia01));
3640
assertTrue (ia1.family() == IPAddress::IPv4);
3741
assertTrue (ia1.toString() == "127.0.0.1");
38-
42+
3943
IPAddress ia02 = IPAddress("192.168.1.120");
4044
IPAddress ia2(std::move(ia02));
4145
assertTrue (ia2.family() == IPAddress::IPv4);
@@ -67,7 +71,7 @@ void IPAddressTest::testStringConv6()
6771
IPAddress ia1(std::move(ia01));
6872
assertTrue (ia1.family() == IPAddress::IPv6);
6973
assertTrue (ia1.toString() == "1080::8:600:200a:425c");
70-
74+
7175
IPAddress ia02 = IPAddress("1080::8:600:200A:425C");
7276
IPAddress ia2(std::move(ia02));
7377
assertTrue (ia2.family() == IPAddress::IPv6);
@@ -424,6 +428,44 @@ void IPAddressTest::testClassification6()
424428
assertTrue (!ip10.isOrgLocalMC());
425429
assertTrue (!ip10.isGlobalMC());
426430

431+
NetworkInterface::Map m = NetworkInterface::map(false, false);
432+
for (auto it = m.begin(); it != m.end(); ++it)
433+
{
434+
IPAddress ip11(Poco::format("fe80::1592:96a0:88bf:d2d7%%%s",
435+
it->second.adapterName())); // link local unicast scoped
436+
assertEqual (ip11.scope(), it->second.index());
437+
assertTrue (!ip11.isWildcard());
438+
assertTrue (!ip11.isBroadcast());
439+
assertTrue (!ip11.isLoopback());
440+
assertTrue (!ip11.isMulticast());
441+
assertTrue (ip11.isUnicast());
442+
assertTrue (ip11.isLinkLocal());
443+
assertTrue (!ip11.isSiteLocal());
444+
assertTrue (!ip11.isWellKnownMC());
445+
assertTrue (!ip11.isNodeLocalMC());
446+
assertTrue (!ip11.isLinkLocalMC());
447+
assertTrue (!ip11.isSiteLocalMC());
448+
assertTrue (!ip11.isOrgLocalMC());
449+
assertTrue (!ip11.isGlobalMC());
450+
451+
IPAddress ip12(Poco::format("[fe80::1592:96a0:88bf:d2d7%%%s]",
452+
it->second.adapterName())); // link local unicast scoped
453+
assertEqual (ip12.scope(), it->second.index());
454+
assertTrue (!ip12.isWildcard());
455+
assertTrue (!ip12.isBroadcast());
456+
assertTrue (!ip12.isLoopback());
457+
assertTrue (!ip12.isMulticast());
458+
assertTrue (ip12.isUnicast());
459+
assertTrue (ip12.isLinkLocal());
460+
assertTrue (!ip12.isSiteLocal());
461+
assertTrue (!ip12.isWellKnownMC());
462+
assertTrue (!ip12.isNodeLocalMC());
463+
assertTrue (!ip12.isLinkLocalMC());
464+
assertTrue (!ip12.isSiteLocalMC());
465+
assertTrue (!ip12.isOrgLocalMC());
466+
assertTrue (!ip12.isGlobalMC());
467+
}
468+
427469
IPAddress ip6("fec0::21f:5bff:fec6:6707"); // site local unicast (RFC 4291)
428470
assertTrue (!ip6.isWildcard());
429471
assertTrue (!ip6.isBroadcast());
@@ -690,6 +732,32 @@ void IPAddressTest::testByteOrderMacros()
690732
}
691733

692734

735+
void IPAddressTest::testScoped()
736+
{
737+
#ifdef POCO_HAVE_IPv6
738+
NetworkInterface::Map m = NetworkInterface::map(false, false);
739+
if (m.size() == 0)
740+
{
741+
std::cout << "No network interfaces found." << std::endl;
742+
return;
743+
}
744+
745+
IPAddress ip;
746+
assertFalse (IPAddress::tryParse("fe80::1592:96a0:88bf:d2d7%xyzabc123", ip));
747+
748+
std::string scope;
749+
auto it = m.begin();
750+
auto end = m.end();
751+
for (; it != end; ++it)
752+
{
753+
scope = it->second.adapterName();
754+
assertTrue (IPAddress::tryParse(Poco::format("[fe80::1592:96a0:88bf:d2d7%%%s]", scope), ip));
755+
assertTrue (IPAddress::tryParse(Poco::format("fe80::1592:96a0:88bf:d2d7%%%s", scope), ip));
756+
}
757+
#endif
758+
}
759+
760+
693761
void IPAddressTest::setUp()
694762
{
695763
}
@@ -719,6 +787,7 @@ CppUnit::Test* IPAddressTest::suite()
719787
CppUnit_addTest(pSuite, IPAddressTest, testPrefixLen);
720788
CppUnit_addTest(pSuite, IPAddressTest, testOperators);
721789
CppUnit_addTest(pSuite, IPAddressTest, testByteOrderMacros);
790+
CppUnit_addTest(pSuite, IPAddressTest, testScoped);
722791

723792
return pSuite;
724793
}

Net/testsuite/src/IPAddressTest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class IPAddressTest: public CppUnit::TestCase
3939
void testPrefixLen();
4040
void testOperators();
4141
void testByteOrderMacros();
42+
void testScoped();
4243

4344
void setUp();
4445
void tearDown();

0 commit comments

Comments
 (0)