IPAddress:

- bitwise operators (&,|,^,~)
- BinaryReader/Writer << and >> operators
- force IPv6 always lowercase (RFC 5952)
This commit is contained in:
Aleksandar Fabijanic 2012-07-21 01:45:19 +00:00
parent 4bc7d4aeae
commit 2ebe065a37
5 changed files with 329 additions and 14 deletions

View File

@ -18,6 +18,9 @@ Release 1.5.0 (2012-07-30)
- fixed SF#3538780: SocketAddress needs operator < function
- fixed SF#3538775: Issues building on Fedora/Centos, etc. for AMD64
- fixed SF#3538786: Use size_t for describing data-blocks in DigestEngine
- IPAddress bitwise operators (&,|,^,~)
- IPAddress BinaryReader/Writer << and >> operators
- IPAddress force IPv6 always lowercase (RFC 5952)
Release 1.4.4 (2012-07-??)
==========================

View File

@ -45,6 +45,10 @@
namespace Poco {
class BinaryReader;
class BinaryWriter;
namespace Net {
@ -121,7 +125,7 @@ public:
#if defined(_WIN32)
IPAddress(const SOCKET_ADDRESS& socket_address);
/// Creates an IPAddress from a SOCKET_ADDRESS wrapper for Windows.
/// Creates an IPAddress from Windows SOCKET_ADDRESS structure.
#endif
IPAddress(const struct sockaddr& sockaddr);
@ -308,6 +312,10 @@ public:
bool operator <= (const IPAddress& addr) const;
bool operator > (const IPAddress& addr) const;
bool operator >= (const IPAddress& addr) const;
IPAddress operator & (const IPAddress& addr) const;
IPAddress operator | (const IPAddress& addr) const;
IPAddress operator ^ (const IPAddress& addr) const;
IPAddress operator ~ () const;
poco_socklen_t length() const;
/// Returns the length in bytes of the internal socket address structure.
@ -386,6 +394,9 @@ inline void swap(IPAddress& addr1, IPAddress& addr2)
}
BinaryWriter& operator << (BinaryWriter& writer, const IPAddress& value);
BinaryReader& operator >> (BinaryReader& reader, IPAddress& value);
} } // namespace Poco::Net

View File

@ -38,13 +38,17 @@
#include "Poco/Net/NetException.h"
#include "Poco/RefCountedObject.h"
#include "Poco/NumberFormatter.h"
#include "Poco/BinaryReader.h"
#include "Poco/BinaryWriter.h"
#include "Poco/String.h"
#include "Poco/Types.h"
#include <algorithm>
#include <cstring>
using Poco::RefCountedObject;
using Poco::NumberFormatter;
using Poco::BinaryReader;
using Poco::BinaryWriter;
using Poco::toLower;
using Poco::UInt8;
using Poco::UInt16;
using Poco::UInt32;
@ -279,8 +283,43 @@ public:
{
return new IPv4AddressImpl(&_addr);
}
private:
IPv4AddressImpl operator & (const IPv4AddressImpl& addr) const
{
IPv4AddressImpl result(&_addr);
result._addr.s_addr &= addr._addr.s_addr;
return result;
}
IPv4AddressImpl operator | (const IPv4AddressImpl& addr) const
{
IPv4AddressImpl result(&_addr);
result._addr.s_addr |= addr._addr.s_addr;
return result;
}
IPv4AddressImpl operator ^ (const IPv4AddressImpl& addr) const
{
IPv4AddressImpl result(&_addr);
result._addr.s_addr ^= addr._addr.s_addr;
return result;
}
IPv4AddressImpl operator ~ () const
{
IPv4AddressImpl result(&_addr);
result._addr.s_addr ^= 0xffffffff;
return result;
}
private:
IPv4AddressImpl(const IPv4AddressImpl& addr)
{
std::memcpy(&_addr, &addr._addr, sizeof(_addr));
}
IPv4AddressImpl& operator = (const IPv4AddressImpl&);
struct in_addr _addr;
};
@ -341,7 +380,7 @@ public:
if (words[5] == 0)
result.append("::");
else
result.append("::FFFF:");
result.append("::ffff:");
const UInt8* bytes = reinterpret_cast<const UInt8*>(&_addr);
NumberFormatter::append(result, bytes[12]);
result.append(".");
@ -391,7 +430,7 @@ public:
}
#endif
}
return result;
return toLower(result);
}
}
@ -557,8 +596,131 @@ public:
return new IPv6AddressImpl(&_addr, _scope);
}
IPv6AddressImpl operator & (const IPv6AddressImpl& addr) const
{
IPv6AddressImpl result(&_addr);
#ifdef POCO_OS_FAMILY_WINDOWS
result._addr.s6_addr[0] &= addr._addr.s6_addr[0];
result._addr.s6_addr[1] &= addr._addr.s6_addr[1];
result._addr.s6_addr[2] &= addr._addr.s6_addr[2];
result._addr.s6_addr[3] &= addr._addr.s6_addr[3];
result._addr.s6_addr[4] &= addr._addr.s6_addr[4];
result._addr.s6_addr[5] &= addr._addr.s6_addr[5];
result._addr.s6_addr[6] &= addr._addr.s6_addr[6];
result._addr.s6_addr[7] &= addr._addr.s6_addr[7];
result._addr.s6_addr[8] &= addr._addr.s6_addr[8];
result._addr.s6_addr[9] &= addr._addr.s6_addr[9];
result._addr.s6_addr[10] &= addr._addr.s6_addr[10];
result._addr.s6_addr[11] &= addr._addr.s6_addr[11];
result._addr.s6_addr[12] &= addr._addr.s6_addr[12];
result._addr.s6_addr[13] &= addr._addr.s6_addr[13];
result._addr.s6_addr[14] &= addr._addr.s6_addr[14];
result._addr.s6_addr[15] &= addr._addr.s6_addr[15];
#else
result._addr.s6_addr32[0] &= addr._addr.s6_addr32[0];
result._addr.s6_addr32[1] &= addr._addr.s6_addr32[1];
result._addr.s6_addr32[2] &= addr._addr.s6_addr32[2];
result._addr.s6_addr32[3] &= addr._addr.s6_addr32[3];
#endif
return result;
}
IPv6AddressImpl operator | (const IPv6AddressImpl& addr) const
{
IPv6AddressImpl result(&_addr);
#ifdef POCO_OS_FAMILY_WINDOWS
result._addr.s6_addr[0] |= addr._addr.s6_addr[0];
result._addr.s6_addr[1] |= addr._addr.s6_addr[1];
result._addr.s6_addr[2] |= addr._addr.s6_addr[2];
result._addr.s6_addr[3] |= addr._addr.s6_addr[3];
result._addr.s6_addr[4] |= addr._addr.s6_addr[4];
result._addr.s6_addr[5] |= addr._addr.s6_addr[5];
result._addr.s6_addr[6] |= addr._addr.s6_addr[6];
result._addr.s6_addr[7] |= addr._addr.s6_addr[7];
result._addr.s6_addr[8] |= addr._addr.s6_addr[8];
result._addr.s6_addr[9] |= addr._addr.s6_addr[9];
result._addr.s6_addr[10] |= addr._addr.s6_addr[10];
result._addr.s6_addr[11] |= addr._addr.s6_addr[11];
result._addr.s6_addr[12] |= addr._addr.s6_addr[12];
result._addr.s6_addr[13] |= addr._addr.s6_addr[13];
result._addr.s6_addr[14] |= addr._addr.s6_addr[14];
result._addr.s6_addr[15] |= addr._addr.s6_addr[15];
#else
result._addr.s6_addr32[0] |= addr._addr.s6_addr32[0];
result._addr.s6_addr32[1] |= addr._addr.s6_addr32[1];
result._addr.s6_addr32[2] |= addr._addr.s6_addr32[2];
result._addr.s6_addr32[3] |= addr._addr.s6_addr32[3];
#endif
return result;
}
IPv6AddressImpl operator ^ (const IPv6AddressImpl& addr) const
{
IPv6AddressImpl result(&_addr);
#ifdef POCO_OS_FAMILY_WINDOWS
result._addr.s6_addr[0] ^= addr._addr.s6_addr[0];
result._addr.s6_addr[1] ^= addr._addr.s6_addr[1];
result._addr.s6_addr[2] ^= addr._addr.s6_addr[2];
result._addr.s6_addr[3] ^= addr._addr.s6_addr[3];
result._addr.s6_addr[4] ^= addr._addr.s6_addr[4];
result._addr.s6_addr[5] ^= addr._addr.s6_addr[5];
result._addr.s6_addr[6] ^= addr._addr.s6_addr[6];
result._addr.s6_addr[7] ^= addr._addr.s6_addr[7];
result._addr.s6_addr[8] ^= addr._addr.s6_addr[8];
result._addr.s6_addr[9] ^= addr._addr.s6_addr[9];
result._addr.s6_addr[10] ^= addr._addr.s6_addr[10];
result._addr.s6_addr[11] ^= addr._addr.s6_addr[11];
result._addr.s6_addr[12] ^= addr._addr.s6_addr[12];
result._addr.s6_addr[13] ^= addr._addr.s6_addr[13];
result._addr.s6_addr[14] ^= addr._addr.s6_addr[14];
result._addr.s6_addr[15] ^= addr._addr.s6_addr[15];
#else
result._addr.s6_addr32[0] ^= addr._addr.s6_addr32[0];
result._addr.s6_addr32[1] ^= addr._addr.s6_addr32[1];
result._addr.s6_addr32[2] ^= addr._addr.s6_addr32[2];
result._addr.s6_addr32[3] ^= addr._addr.s6_addr32[3];
#endif
return result;
}
IPv6AddressImpl operator ~ () const
{
IPv6AddressImpl result(&_addr);
#ifdef POCO_OS_FAMILY_WINDOWS
result._addr.s6_addr[0] ^= 0xff;
result._addr.s6_addr[1] ^= 0xff;
result._addr.s6_addr[2] ^= 0xff;
result._addr.s6_addr[3] ^= 0xff;
result._addr.s6_addr[4] ^= 0xff;
result._addr.s6_addr[5] ^= 0xff;
result._addr.s6_addr[6] ^= 0xff;
result._addr.s6_addr[7] ^= 0xff;
result._addr.s6_addr[8] ^= 0xff;
result._addr.s6_addr[9] ^= 0xff;
result._addr.s6_addr[10] ^= 0xff;
result._addr.s6_addr[11] ^= 0xff;
result._addr.s6_addr[12] ^= 0xff;
result._addr.s6_addr[13] ^= 0xff;
result._addr.s6_addr[14] ^= 0xff;
result._addr.s6_addr[15] ^= 0xff;
#else
result._addr.s6_addr32[0] ^= 0xffffffff;
result._addr.s6_addr32[1] ^= 0xffffffff;
result._addr.s6_addr32[2] ^= 0xffffffff;
result._addr.s6_addr32[3] ^= 0xffffffff;
#endif
return result;
}
private:
struct in6_addr _addr;
IPv6AddressImpl(const IPv6AddressImpl& addr): _scope(0)
{
std::memcpy((void*) &_addr, (void*) &addr._addr, sizeof(_addr));
}
IPv6AddressImpl& operator = (const IPv6AddressImpl&);
struct in6_addr _addr;
Poco::UInt32 _scope;
};
@ -890,6 +1052,103 @@ bool IPAddress::operator >= (const IPAddress& a) const
}
IPAddress IPAddress::operator & (const IPAddress& other) const
{
if (family() == other.family())
{
if (family() == IPv4)
{
IPv4AddressImpl self(_pImpl->addr());
IPv4AddressImpl other(other._pImpl->addr());
return IPAddress((self & other).addr(), sizeof(struct in_addr));
}
#if defined(POCO_HAVE_IPv6)
else if (family() == IPv6)
{
IPv6AddressImpl self(_pImpl->addr());
IPv6AddressImpl other(other._pImpl->addr());
return IPAddress((self & other).addr(), sizeof(struct in6_addr));
}
#endif
else
throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()");
}
else
throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()");
}
IPAddress IPAddress::operator | (const IPAddress& other) const
{
if (family() == other.family())
{
if (family() == IPv4)
{
IPv4AddressImpl self(_pImpl->addr());
IPv4AddressImpl other(other._pImpl->addr());
return IPAddress((self | other).addr(), sizeof(struct in_addr));
}
#if defined(POCO_HAVE_IPv6)
else if (family() == IPv6)
{
IPv6AddressImpl self(_pImpl->addr());
IPv6AddressImpl other(other._pImpl->addr());
return IPAddress((self | other).addr(), sizeof(struct in6_addr));
}
#endif
else
throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()");
}
else
throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()");
}
IPAddress IPAddress::operator ^ (const IPAddress& other) const
{
if (family() == other.family())
{
if (family() == IPv4)
{
IPv4AddressImpl self(_pImpl->addr());
IPv4AddressImpl other(other._pImpl->addr());
return IPAddress((self ^ other).addr(), sizeof(struct in_addr));
}
#if defined(POCO_HAVE_IPv6)
else if (family() == IPv6)
{
IPv6AddressImpl self(_pImpl->addr());
IPv6AddressImpl other(other._pImpl->addr());
return IPAddress((self ^ other).addr(), sizeof(struct in6_addr));
}
#endif
else
throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()");
}
else
throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()");
}
IPAddress IPAddress::operator ~ () const
{
if (family() == IPv4)
{
IPv4AddressImpl self(_pImpl->addr());
return IPAddress((~self).addr(), sizeof(struct in_addr));
}
#if defined(POCO_HAVE_IPv6)
else if (family() == IPv6)
{
IPv6AddressImpl self(_pImpl->addr());
return IPAddress((~self).addr(), sizeof(struct in6_addr));
}
#endif
else
throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()");
}
poco_socklen_t IPAddress::length() const
{
return _pImpl->length();
@ -969,4 +1228,19 @@ IPAddress IPAddress::broadcast()
}
BinaryWriter& operator << (BinaryWriter& writer, const IPAddress& value)
{
writer.stream().write((const char*) value.addr(), value.length());
return writer;
}
BinaryReader& operator >> (BinaryReader& reader, IPAddress& value)
{
char buf[sizeof(struct in6_addr)];
reader.stream().read(buf, value.length());
value = IPAddress(buf, value.length());
return reader;
}
} } // namespace Poco::Net

View File

@ -78,25 +78,25 @@ void IPAddressTest::testStringConv()
void IPAddressTest::testStringConv6()
{
#ifdef POCO_HAVE_IPv6
IPAddress ia1("1080:0:0:0:8:600:200A:425C");
IPAddress ia1("1080:0:0:0:8:600:200a:425c");
assert (ia1.family() == IPAddress::IPv6);
assert (ia1.toString() == "1080::8:600:200A:425C");
assert (ia1.toString() == "1080::8:600:200a:425c");
IPAddress ia2("1080::8:600:200A:425C");
assert (ia2.family() == IPAddress::IPv6);
assert (ia2.toString() == "1080::8:600:200A:425C");
assert (ia2.toString() == "1080::8:600:200a:425c");
IPAddress ia3("::192.168.1.120");
assert (ia3.family() == IPAddress::IPv6);
assert (ia3.toString() == "::192.168.1.120");
IPAddress ia4("::FFFF:192.168.1.120");
IPAddress ia4("::ffff:192.168.1.120");
assert (ia4.family() == IPAddress::IPv6);
assert (ia4.toString() == "::FFFF:192.168.1.120");
assert (ia4.toString() == "::ffff:192.168.1.120");
IPAddress ia5(64, IPAddress::IPv6);
assert (ia5.family() == IPAddress::IPv6);
assert (ia5.toString() == "FFFF:FFFF:FFFF:FFFF::");
assert (ia5.toString() == "ffff:ffff:ffff:ffff::");
#endif
}
@ -544,6 +544,29 @@ void IPAddressTest::testBroadcast()
}
void IPAddressTest::testPrefixCons()
{
IPAddress mask(15, IPAddress::IPv4);
assert(mask.toString() == "255.254.0.0");
}
void IPAddressTest::testOperators()
{
IPAddress ip("10.0.0.51");
IPAddress mask(24, IPAddress::IPv4);
IPAddress net = ip & mask;
assert(net.toString() == "10.0.0.0");
IPAddress host("0.0.0.51");
assert((net | host) == ip);
assert((~mask).toString() == "0.0.0.255");
}
void IPAddressTest::testRelationals6()
{
#ifdef POCO_HAVE_IPv6
@ -576,6 +599,8 @@ CppUnit::Test* IPAddressTest::suite()
CppUnit_addTest(pSuite, IPAddressTest, testRelationals6);
CppUnit_addTest(pSuite, IPAddressTest, testWildcard);
CppUnit_addTest(pSuite, IPAddressTest, testBroadcast);
CppUnit_addTest(pSuite, IPAddressTest, testPrefixCons);
CppUnit_addTest(pSuite, IPAddressTest, testOperators);
return pSuite;
}

View File

@ -57,6 +57,8 @@ public:
void testRelationals6();
void testWildcard();
void testBroadcast();
void testPrefixCons();
void testOperators();
void setUp();
void tearDown();