diff --git a/Net/src/IPAddress.cpp b/Net/src/IPAddress.cpp index 211f35de8..783d90a81 100644 --- a/Net/src/IPAddress.cpp +++ b/Net/src/IPAddress.cpp @@ -128,7 +128,7 @@ IPAddress::IPAddress(const std::string& addr, Family family) IPAddress::IPAddress(const void* addr, poco_socklen_t length) #ifndef POCO_HAVE_ALIGNMENT -: _pImpl(0) + : _pImpl(0) #endif { if (length == sizeof(struct in_addr)) @@ -178,7 +178,7 @@ IPAddress::IPAddress(unsigned prefix, Family family) #if defined(_WIN32) IPAddress::IPAddress(const SOCKET_ADDRESS& socket_address) #ifndef POCO_HAVE_ALIGNMENT -: _pImpl(0) + : _pImpl(0) #endif { ADDRESS_FAMILY family = socket_address.lpSockaddr->sa_family; @@ -187,7 +187,7 @@ IPAddress::IPAddress(const SOCKET_ADDRESS& socket_address) #if defined(POCO_HAVE_IPv6) else if (family == AF_INET6) newIPv6(&reinterpret_cast(socket_address.lpSockaddr)->sin6_addr, - reinterpret_cast(socket_address.lpSockaddr)->sin6_scope_id); + reinterpret_cast(socket_address.lpSockaddr)->sin6_scope_id); #endif else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); } @@ -202,7 +202,7 @@ IPAddress::IPAddress(const struct sockaddr& sockaddr) #if defined(POCO_HAVE_IPv6) else if (family == AF_INET6) newIPv6(&reinterpret_cast(&sockaddr)->sin6_addr, - reinterpret_cast(&sockaddr)->sin6_scope_id); + reinterpret_cast(&sockaddr)->sin6_scope_id); #endif else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); } @@ -348,8 +348,7 @@ bool IPAddress::operator == (const IPAddress& a) const #endif return std::memcmp(addr(), a.addr(), l1) == 0; } - else - return false; + else return false; } @@ -371,8 +370,7 @@ bool IPAddress::operator < (const IPAddress& a) const #endif return std::memcmp(addr(), a.addr(), l1) < 0; } - else - return l1 < l2; + else return l1 < l2; } @@ -413,11 +411,9 @@ IPAddress IPAddress::operator & (const IPAddress& other) const return IPAddress(r.addr(), r.scope(), 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()"); } - else - throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); + else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); } @@ -440,11 +436,9 @@ IPAddress IPAddress::operator | (const IPAddress& other) const return IPAddress(r.addr(), r.scope(), 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()"); } - else - throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); + else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); } @@ -467,11 +461,9 @@ IPAddress IPAddress::operator ^ (const IPAddress& other) const return IPAddress(r.addr(), r.scope(), 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()"); } - else - throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); + else throw Poco::InvalidArgumentException("Invalid or unsupported address family passed to IPAddress()"); } @@ -490,8 +482,7 @@ IPAddress IPAddress::operator ~ () const return IPAddress(r.addr(), sizeof(struct in6_addr), r.scope()); } #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()"); } @@ -525,7 +516,7 @@ IPAddress IPAddress::parse(const std::string& addr) } - bool IPAddress::tryParse(const std::string& addr, IPAddress& result) +bool IPAddress::tryParse(const std::string& addr, IPAddress& result) { IPv4AddressImpl impl4(IPv4AddressImpl::parse(addr)); if (impl4 != IPv4AddressImpl() || trim(addr) == "0.0.0.0") @@ -541,7 +532,7 @@ IPAddress IPAddress::parse(const std::string& addr) return true; } #endif - return false; + return false; } diff --git a/Net/src/IPAddressImpl.cpp b/Net/src/IPAddressImpl.cpp index 20b250436..f324c6015 100644 --- a/Net/src/IPAddressImpl.cpp +++ b/Net/src/IPAddressImpl.cpp @@ -18,6 +18,7 @@ #include "Poco/Net/NetException.h" #include "Poco/RefCountedObject.h" #include "Poco/NumberFormatter.h" +#include "Poco/ByteOrder.h" #include "Poco/String.h" #include "Poco/Types.h" @@ -32,6 +33,7 @@ using Poco::UInt32; namespace { + template unsigned maskBits(T val, unsigned size) /// Returns the length of the mask (number of bits set in val). @@ -49,6 +51,7 @@ unsigned maskBits(T val, unsigned size) return size - count; } + } // namespace @@ -92,7 +95,7 @@ IPv4AddressImpl::IPv4AddressImpl(const void* addr) IPv4AddressImpl::IPv4AddressImpl(unsigned prefix) { UInt32 addr = (prefix == 32) ? 0xffffffff : ~(0xffffffff >> prefix); - _addr.s_addr = htonl(addr); + _addr.s_addr = ByteOrder::toNetwork(addr); } @@ -388,21 +391,31 @@ IPv6AddressImpl::IPv6AddressImpl(unsigned prefix): { 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(~(0xffff >> prefix)); + { + _addr.s6_addr16[i++] = ByteOrder::toNetwork(~(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) - _addr.s6_addr32[i++] = htonl(~(0xffffffffU >> prefix)); + { + _addr.s6_addr32[i++] = ByteOrder::toNetwork(~(0xffffffffU >> prefix)); + } while (i < 4) + { _addr.s6_addr32[i++] = 0; + } #endif } @@ -448,7 +461,7 @@ std::string IPv6AddressImpl::toString() const } } if (i > 0) result.append(":"); - if (i < 8) NumberFormatter::appendHex(result, ntohs(words[i++])); + if (i < 8) NumberFormatter::appendHex(result, ByteOrder::fromNetwork(words[i++])); } if (_scope > 0) { @@ -511,7 +524,7 @@ unsigned IPv6AddressImpl::prefixLength() const #elif defined(POCO_OS_FAMILY_WINDOWS) for (int i = 7; i >= 0; --i) { - unsigned short addr = ntohs(_addr.s6_addr16[i]); + unsigned short addr = ByteOrder::fromNetwork(_addr.s6_addr16[i]); if ((bits = maskBits(addr, 16))) return (bitPos - (16 - bits)); bitPos -= 16; } @@ -545,28 +558,28 @@ bool IPv6AddressImpl::isLoopback() const { const UInt16* words = reinterpret_cast(&_addr); return words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && - words[4] == 0 && words[5] == 0 && words[6] == 0 && ntohs(words[7]) == 0x0001; + words[4] == 0 && words[5] == 0 && words[6] == 0 && ByteOrder::fromNetwork(words[7]) == 0x0001; } bool IPv6AddressImpl::isMulticast() const { const UInt16* words = reinterpret_cast(&_addr); - return (ntohs(words[0]) & 0xFFE0) == 0xFF00; + return (ByteOrder::fromNetwork(words[0]) & 0xFFE0) == 0xFF00; } bool IPv6AddressImpl::isLinkLocal() const { const UInt16* words = reinterpret_cast(&_addr); - return (ntohs(words[0]) & 0xFFE0) == 0xFE80; + return (ByteOrder::fromNetwork(words[0]) & 0xFFE0) == 0xFE80; } bool IPv6AddressImpl::isSiteLocal() const { const UInt16* words = reinterpret_cast(&_addr); - return ((ntohs(words[0]) & 0xFFE0) == 0xFEC0) || ((ntohs(words[0]) & 0xFF00) == 0xFC00); + return ((ByteOrder::fromNetwork(words[0]) & 0xFFE0) == 0xFEC0) || ((ByteOrder::fromNetwork(words[0]) & 0xFF00) == 0xFC00); } @@ -580,49 +593,49 @@ bool IPv6AddressImpl::isIPv4Compatible() const bool IPv6AddressImpl::isIPv4Mapped() const { const UInt16* words = reinterpret_cast(&_addr); - return words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && words[4] == 0 && ntohs(words[5]) == 0xFFFF; + return words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && words[4] == 0 && ByteOrder::fromNetwork(words[5]) == 0xFFFF; } bool IPv6AddressImpl::isWellKnownMC() const { const UInt16* words = reinterpret_cast(&_addr); - return (ntohs(words[0]) & 0xFFF0) == 0xFF00; + return (ByteOrder::fromNetwork(words[0]) & 0xFFF0) == 0xFF00; } bool IPv6AddressImpl::isNodeLocalMC() const { const UInt16* words = reinterpret_cast(&_addr); - return (ntohs(words[0]) & 0xFFEF) == 0xFF01; + return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF01; } bool IPv6AddressImpl::isLinkLocalMC() const { const UInt16* words = reinterpret_cast(&_addr); - return (ntohs(words[0]) & 0xFFEF) == 0xFF02; + return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF02; } bool IPv6AddressImpl::isSiteLocalMC() const { const UInt16* words = reinterpret_cast(&_addr); - return (ntohs(words[0]) & 0xFFEF) == 0xFF05; + return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF05; } bool IPv6AddressImpl::isOrgLocalMC() const { const UInt16* words = reinterpret_cast(&_addr); - return (ntohs(words[0]) & 0xFFEF) == 0xFF08; + return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF08; } bool IPv6AddressImpl::isGlobalMC() const { const UInt16* words = reinterpret_cast(&_addr); - return (ntohs(words[0]) & 0xFFEF) == 0xFF0F; + return (ByteOrder::fromNetwork(words[0]) & 0xFFEF) == 0xFF0F; } @@ -683,10 +696,8 @@ IPAddressImpl* IPv6AddressImpl::clone() const IPv6AddressImpl IPv6AddressImpl::operator & (const IPv6AddressImpl& addr) const { - if (_scope != addr._scope) { - throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match " - "with the source one."); - } + if (_scope != addr._scope) + throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match with the source one."); IPv6AddressImpl result(*this); #ifdef POCO_OS_FAMILY_WINDOWS @@ -710,10 +721,8 @@ IPv6AddressImpl IPv6AddressImpl::operator & (const IPv6AddressImpl& addr) const IPv6AddressImpl IPv6AddressImpl::operator | (const IPv6AddressImpl& addr) const { - if (_scope != addr._scope) { - throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match " - "with the source one."); - } + if (_scope != addr._scope) + throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match with the source one."); IPv6AddressImpl result(*this); #ifdef POCO_OS_FAMILY_WINDOWS @@ -737,10 +746,8 @@ IPv6AddressImpl IPv6AddressImpl::operator | (const IPv6AddressImpl& addr) const IPv6AddressImpl IPv6AddressImpl::operator ^ (const IPv6AddressImpl& addr) const { - if (_scope != addr._scope) { - throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match " - "with the source one."); - } + if (_scope != addr._scope) + throw Poco::InvalidArgumentException("Scope ID of passed IPv6 address does not match with the source one."); IPv6AddressImpl result(*this);