fix for shifting prefix bits

This commit is contained in:
Aleksandar Fabijanic 2012-07-31 03:50:28 +00:00
parent 219a945cc9
commit d1a1d54f58
2 changed files with 32 additions and 7 deletions

View File

@ -149,7 +149,7 @@ public:
IPv4AddressImpl(unsigned prefix)
{
UInt32 addr = ~(0xffffffff >> prefix);
UInt32 addr = (prefix == 32) ? 0xffffffff : ~(0xffffffff >> prefix);
_addr.s_addr = htonl(addr);
}
@ -383,15 +383,15 @@ public:
{
unsigned i = 0;
#ifdef POCO_OS_FAMILY_WINDOWS
for (; prefix > 16; ++i, prefix -= 16) {
for (; prefix >= 16; ++i, prefix -= 16) {
_addr.s6_addr16[i] = 0xffff;
}
if (prefix > 0)
_addr.s6_addr16[i++] = htons(~(0xffffU >> prefix));
_addr.s6_addr16[i++] = htons(~(0xffff >> prefix));
while (i < 8)
_addr.s6_addr16[i++] = 0;
#else
for (; prefix > 32; ++i, prefix -= 32) {
for (; prefix >= 32; ++i, prefix -= 32) {
_addr.s6_addr32[i] = 0xffffffff;
}
if (prefix > 0)

View File

@ -97,6 +97,10 @@ void IPAddressTest::testStringConv6()
IPAddress ia5(64, IPAddress::IPv6);
assert (ia5.family() == IPAddress::IPv6);
assert (ia5.toString() == "ffff:ffff:ffff:ffff::");
IPAddress ia6(32, IPAddress::IPv6);
assert (ia6.family() == IPAddress::IPv6);
assert (ia6.toString() == "ffff:ffff::");
#endif
}
@ -557,10 +561,31 @@ void IPAddressTest::testPrefixCons()
void IPAddressTest::testPrefixLen()
{
IPAddress ia1(15, IPAddress::IPv4);
IPAddress ia2(62, IPAddress::IPv6);
assert(ia1.prefixLength() == 15);
assert(ia2.prefixLength() == 62);
IPAddress ia2(16, IPAddress::IPv4);
assert(ia2.prefixLength() == 16);
IPAddress ia3(23, IPAddress::IPv4);
assert(ia3.prefixLength() == 23);
IPAddress ia4(24, IPAddress::IPv4);
assert(ia4.prefixLength() == 24);
IPAddress ia5(25, IPAddress::IPv4);
assert(ia5.prefixLength() == 25);
IPAddress ia6(62, IPAddress::IPv6);
assert(ia6.prefixLength() == 62);
IPAddress ia7(63, IPAddress::IPv6);
assert(ia7.prefixLength() == 63);
IPAddress ia8(64, IPAddress::IPv6);
assert(ia8.prefixLength() == 64);
IPAddress ia9(65, IPAddress::IPv6);
assert(ia9.prefixLength() == 65);
}